Is it possible to disable all Debug.logs in builds?

Pretty sure the answer is “no”, but is it possible to disable all logging when making final release builds? We could of course go through our game and remove/disable all the Debug.logs that we create, but we’re using a lot of plugins and DLLs that have their own logs, and Unity itself prints some things to the log.

The trick is, whenever there’s a Debug.log it seems to open the log file, write something like 10 lines to it (call stack for the Debug.log), then close it. On certain devices (like iPhone) this is a slow process that can cause framerate issues, and I wouldn’t be surprised if the file can get large enough to start affecting memory as well. And the log isn’t really all that useful to us in release builds, unless we can convince a user to go through all the steps to pull it from the device and send it to us.

The Application class has a static LogCallback delegate and 2 LogMessageReceived events to listen for. I’m not sure if you can disable debug logs, but it’s likely through either the Debug or Application class.

What I would do in your situation is make your OWN log class so you can call MyDebug.Log() and pass the results from there to the normal Debug.Log() unless you toggle a “ReleaseBuild” bool. Once you write that class, all you need to do is a “Find&Replace All” for your whole project, replacing Debug.log with MyDebug.Log().

Yeah write your own wrapper and flag the method with System.Diagnostics.ConditionalAttribute Technical documentation | Microsoft Learn. Use either one of unitys conditionals or create your own. That way the compiler schould remove all calls to the method

Check out the other thread, where i already answered and supplied a code example on how this can be achieved: Strip release build from all Debug.Log calls - Unity Engine - Unity Discussions

1 Like

Unfortunately, that doesn’t really help me, because I don’t want to remove Debug.Logs, I want them to just not write to the log file in release builds, something that should in theory be an easy thing for the Unity guys to implement. Because as I said, we use a LOT of plugins, I don’t want to edit code in plugins, also some Debug.logs are hidden in DLLs, and Unity itself writes data to the console (and thus the log file), even in release builds. We actually created our own DebugLog class long ago (that unfortunately not everyone used in the long time since we started the project), but that doesn’t completely solve the problem.

What do you mean write to the log file ?

On Android, for example, you see Unity’s logs in logcat, but i don’t think these are saved to any file…

What would you like to achieve ?

Dunno about Android, but at least on iOS, every Debug.Log is written to a log file, which is why having lots of Debug.Logs can really kill the framerate, as it is accessing the file system. And I’m assuming it affects memory as well, since it has to open this file which could get rather large after an extended play session.

So why wouldn’t the suggested solution work ?

In release builds - all the debug.logs will be gone, automatically.

For other builds you will still have them.

In my project, i use this plugin to disable logs in builds. Just 1-click to enable or disable all logs.

To disable logging completely use:
Debug.logger.logEnabled=false;

To enable it use:
Debug.logger.logEnabled=true;
(Source: Unity - Scripting API: Debug and https://docs.unity3d.com/ScriptReference/Debug-logger.html)

If you want logging dependent on development/release build or platform depending “You can also use the DEVELOPMENT_BUILD #define directive to identify whether your script is running in a player which was built with the “Development Build” option enabled.” (Source: Unity - Manual: Conditional compilation)
The option can be en- or disabled: "In the Build Settings dialog there is a check box called “Development Build”. (Source: Unity - Scripting API: Debug).

And all together for development or release builds
#if DEVELOPMENT_BUILD
Debug.logger.logEnabled=true;
#else
Debug.logger.logEnabled=false;
#endif

… or different platforms:
#if UNITY_EDITOR
Debug.logger.logEnabled=true;
#elseif UNITY_IPHONE
Debug.logger.logEnabled=false;
#elseif UNITY_STANDALONE_OSX
Debug.logger.logEnabled=false;
#endif

18 Likes

I wonder which Unity version introduced the Debug.logger field … i wasn’t aware of its existence !!

1 Like

It didn’t exist until Unity 5.3.

–Eric

2 Likes

Hi,
I know its an old forum. But I had a doubt regarding disabling logs. if we disable logs "Debug.logger.logEnabled=false;", will it only disable the displaying of logs or will it disable the logs internally?
Basically, is "Debug.logger.logEnabled=false;" equivalent to removing all the log statements in your scripts manually?

1 Like

Wonder that aswell!

What exactly is your question ?

Didn’t realise this functionality existed. Thanks.

Excatly what I quoted.
Does setting logEnabled to false is equivalent to removing the log statements (aka doesnt enter to compilation proccess) ?

To answer your question: It’s not equivalent to removing the statement because the method is still called. This means that any string concatenations, etc in the parameter list will still be executed too.

How did you verify it?

Debug.logger.logEnabled = false;
Debug.Log(SomeFunc());

SomeFunc() was called.

1 Like