Disable Logging in Release Build in Unity

Prasetio Nugroho
4 min readOct 18, 2020

How often do you use Debug.Log(string) in unity when you code in your game using Unity? a lot? I think programmer will use debug.log

What is debug.log?

Logs a message to the Unity Console. Use Debug.Log to print informational messages that help you debug your application. For example, you could print a message containing a GameObject.name and information about the object’s current state. — Debug.Log Documentation

This is very useful for you if you stuck or don’t know what should you do in your code. First thing I recommend is debugging.

Example

I have made unity project sample related in this. You can download it in github.

Let’s say, i want to call method or function when i click the button. I made a button in a scene.

button in scene

Then, i created a script then add it to the button. In my script there is debug log, to make sure when i click, the method is called.

When i play and click the button, there is log in console that say “button clicked”

log console button clicked

Ok, everything looks good.

Did you know that if we use debug.log will be effect your game/app performance?

Based on unity article, if we don’t use debug.log anymore, we should remove it in our code. Because it will affect in our performance.

We should remove calls to Debug.Log() as soon as they are no longer needed for debugging purposes. Calls to Debug.Log() still execute in all builds of our game, even if they do not output to anything. A call to Debug.Log() creates and disposes of at least one string, so if our game contains many of these calls, the garbage can add up. — unity: fixing performance problems

Then, how to remove debug.log call effectively in our code?

How to remove(1)

After research, the effective way to remove debug.log call in our code is using Unity’s Platform Dependent Compilation feature.

Unity’s Platform Dependent Compilation feature consists of some preprocessor directives that let you partition your scripts
to compile and execute a section of code exclusively for one of the supported platforms.

The result is like this, i try to disable log in unity editor

And when i play and look the console in editor, no log appear, Great!!

no log appear

How to remove(2)

There is another way to remove our debug.log call. The first way, i add Platform dependent compilation, so i should check the platform and adding the if statement for each code file that i already created. But, in second way, it uses ConditionalAttribute and adding the proper compiler define symbol for the logger. And it will apply automatically for all code file. Thanks to the people form the forum that share this method.

I added the logger.cs that i get from unity forum. Then, in project setting i added string in scriptings define symbols.

scriptings define symbols

After that, i call logger to debug.

I play in editor, and there is a log in console.

log in console

Let’s build on Android. open the adb to check the log. I already tap the button, and log is appear.

logcat android

Looks correct for me, because i want to enable the log. So i try to disable by edit scriptings define symbols from ENABLE_LOG into DISABLE_LOG, and build again and check again in logcat.

no log appear

Yeay!! No log appear.

Reference

Unity learn

Thanks to someone in forum that share the knowledge about this.

https://forum.unity.com/threads/strip-release-build-from-all-debug-log-calls.353600/?_ga=2.32046590.1137475135.1602800058-37752069.1576933713

--

--

Prasetio Nugroho

Interested in new tech, gaming, programming and another knowledge or possibilites that can make me better. Cheers!!