Search Unity

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

Discussion in 'Editor & General Support' started by Dreamwriter, Oct 16, 2015.

  1. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    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.
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    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().
     
  3. Deleted User

    Deleted User

    Guest

    Yeah write your own wrapper and flag the method with System.Diagnostics.ConditionalAttribute https://msdn.microsoft.com/de-de/library. Use either one of unitys conditionals or create your own. That way the compiler schould remove all calls to the method
     
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    holliebuckets likes this.
  5. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    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.
     
  6. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    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 ?
     
  7. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    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.
     
  8. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    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.
     
  9. freshingrain

    freshingrain

    Joined:
    May 31, 2015
    Posts:
    5
    In my project, i use this plugin to disable logs in builds. Just 1-click to enable or disable all logs.
     
    Last edited: Apr 10, 2016
  10. doztep

    doztep

    Joined:
    Sep 8, 2016
    Posts:
    2
    To disable logging completely use:
    Debug.logger.logEnabled=false;

    To enable it use:
    Debug.logger.logEnabled=true;
    (Source: https://docs.unity3d.com/ScriptReference/Debug.html 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: https://docs.unity3d.com/Manual/PlatformDependentCompilation.html)
    The option can be en- or disabled: "In the Build Settings dialog there is a check box called "Development Build". (Source: https://docs.unity3d.com/ScriptReference/Debug.html).

    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
     
  11. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    I wonder which Unity version introduced the Debug.logger field ... i wasn't aware of its existence !!
     
    RakshithAnand likes this.
  12. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It didn't exist until Unity 5.3.

    --Eric
     
    5argon and RakshithAnand like this.
  13. Deleted User

    Deleted User

    Guest

    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?
     
    kobyle likes this.
  14. kobyle

    kobyle

    Joined:
    Feb 23, 2015
    Posts:
    92
    Wonder that aswell!
     
  15. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    What exactly is your question ?
     
  16. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    Didn't realise this functionality existed. Thanks.
     
  17. kobyle

    kobyle

    Joined:
    Feb 23, 2015
    Posts:
    92
    Excatly what I quoted.
    Does setting logEnabled to false is equivalent to removing the log statements (aka doesnt enter to compilation proccess) ?
     
  18. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    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.
     
  19. kobyle

    kobyle

    Joined:
    Feb 23, 2015
    Posts:
    92
    How did you verify it?
     
  20. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    Code (CSharp):
    1.  
    2. Debug.logger.logEnabled = false;
    3. Debug.Log(SomeFunc());
    SomeFunc() was called.
     
    peetonn likes this.
  21. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    If you want to get around this, you'll want to use a Conditional method instead of the suggestions upthread.

    Like this:

    Code (csharp):
    1. public static class MyDebug {
    2.     [Conditional("DEVELOPMENT_BUILD")
    3.     public static string Log(string text) {
    4.         Debug.Log(text);
    5.     }
    6. }
    If the flag DEVELOPMENT_BUILD isn't set, all calls to MyDebug.Log will be skipped when compiling.
     
    kobyle likes this.
  22. kobyle

    kobyle

    Joined:
    Feb 23, 2015
    Posts:
    92
    Thanks I have seen this method for solution, however this wont deal with 3rd party plugins, thus I wished there was a robust solution for Unity to offer.
     
  23. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    There's no way to change what code in third party solutions do without changing those third party solutions. If you have them in source code form, you could write a parser that post-processes that source and strips Debug.Log calls. If you have them in dll form, it'll be much harder.

    Essentially, if you've got a third party plugin that does this:
    Code (csharp):
    1. Debug.Log("Hello, this is version " + versionNumber + " of The_Cool_Plugin");
    You're just going to have to eat that allocation, or send a stern email to your plugin's author.

    Setting logenabled to false will at least prevent file writes, which is the big problem.
     
    kobyle likes this.
  24. kobyle

    kobyle

    Joined:
    Feb 23, 2015
    Posts:
    92
    Thanks, do you have a snippet for that post-process stripping?
     
  25. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Note the italics for could there. I know that it's possible to do things like that, but I have no idea how to go about it. It's probably very hard!
     
  26. ismaelnascimentoash

    ismaelnascimentoash

    Joined:
    Apr 2, 2017
    Posts:
    30
    Very good !
    But current the code is : Debug.unityLogger.logEnabled = false;
     
  27. DavidSWu

    DavidSWu

    Joined:
    Jun 20, 2016
    Posts:
    183
    Best to use a custom debug class as Baste mentioned and then Search and replace in all files if necessary.
    [Conditional()] is wonderful. It strips away any function calls and/or string formatting, and if you have code that checks something before that Log call that has no side effects, the optimizer should get rid of that too.
     
  28. Karsnen_2

    Karsnen_2

    Joined:
    Nov 28, 2011
    Posts:
    89
    Try

    Code (CSharp):
    1. Debug.unityLogger.logEnabled = false;
     
  29. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    what about (ProjectSettings -> Player -> Resolution & Presentation):
    upload_2020-2-27_10-33-18.png
    and (ProjectSettings -> Player -> Other Settings)
    upload_2020-2-27_10-33-45.png

    are these what unity provides to turn off all debugging in a Build?
     
  30. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    Sort of. You can provide your own loggers using the debug API, and those loggers will still work when the player log is disabled. The player log is the output log that is generated automatically by Unity, and disabling it will probably not disable the rest of the Debug system (unless maybe they detect there are no available loggers).

    Disabling all stack traces won't eliminate any log entries, but will make them shorter by excluding the "call stack", which lists all of the functions that needed to be called to get to the line of code that generated the log entry.
     
  31. omar92

    omar92

    Joined:
    Jan 13, 2013
    Posts:
    4
    my solution (Tested on Unity2019.4)

    this will only show exceptions

    you can set it to LogType.Error for exceptions and errors and so on

    #if !(DEVELOPMENT_BUILD || UNITY_EDITOR)
    Debug.unityLogger.filterLogType = LogType.Exception;
    #endif
     
    Last edited: Mar 18, 2021
    AzeriPatates and CYCLE-6 like this.