Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Should you remove all Debug.Logs from final release?

Discussion in 'Scripting' started by yoonitee, Mar 1, 2016.

  1. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,364
    As an example if I just had:

    Update(){
    Debug.Log("Hello world");
    }

    And released the game. Could this end up crashing the game? For example, if the user had a Unity logger switched on and it would create a massive log file?
     
  2. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    It won't crash anything, but it will DEFINITELY slow things down if you're logging every single frame. You should also be considering how your users will feel when their game grows by a gig or two every time they play your game. :p
     
  3. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,364
    I just assumed that when you compile the game for PC it just ignores all the Debug.Logs. I thought they were just for the editor. What happens to them then??? Is there a way to turn off them off globally just in case this kind of thing happens?
     
  4. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
  5. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    The "Hello world" string is 11 characters, to that, add two characters for end of line (supposing the game run on windows) you have a total 13 byte of data.
    Let's say you game run at 60 fps, then you have 13*60=780 byte/s written to the disk when the game is running.

    Let's say the user hard drive is almost full and have 2 GB of freespace left.
    Then the player could play about ((2^30)/780)/(3600*24) ~ 16 days before your game raises a
    System.IO.IOException.

    But don't worry, that's enough time for the player to have fun hearing it's hard drive spinning, get bored and quit the game.
     
  6. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,364
    Ah. I see. So I just should uncheck "Use Player Log" in the settings.

    BTW. I wouldn't actually be silly enough to write code as above. It might be more on the lines of:

    Update(){
    doSomeServerCall();
    }

    OnErrorFromServer(){
    Debug.Log("Server error");
    }

    Which is still pretty silly code. But if I miss it I wouldn't want the game to just freeze due to writing errors to the log file!
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    It wouldn't freeze; it would log the error. And that can be really useful.

    So, in conclusion, you should remove silly/verbose DebugLogs from your builds, but leave useful ones that might help you understand what went wrong when a user sends you a cryptic bug report.
     
    SparrowGS and Joe-Censored like this.
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Plus the stack trace...
     
    ericbegue likes this.
  9. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    :rolleyes: Oh yeah... plus the stacktrace ~ 7 Days.
     
    Kiwasi likes this.
  10. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,364
    What a stacktrace?
     
  11. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    It's a list (a stack) of function calls that ends up to the current function.

    It tells that the current function has been call from function A, which has been called from function B, ..., which has been called from the Main function.

    It traces the stack of function calls.
     
    MoonJellyGames likes this.
  12. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Run a Debug.Log. Click on the message in the console. The stack trace is all of the info down the bottom that tells you where the message comes from. Its building this stack trace that is the slowest part of Debug.Log
     
    ericbegue likes this.
  13. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Honestly, keep debug statements out of update, or anywhere it'll run every frame. I like to use them for state changes, file allocations, corner case behaviours, etc. It is slow...
     
  14. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    Also precede all of your Debug.Log* statements with a check of Debug.isDebugBuild. This lets you make a beta/debug build with debug statements, or a final build without them, without having to touch any code or define symbols.
     
    jtsmith1287 and Kiwasi like this.
  15. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    I was actually unaware of this before I saw it mentioned here. 10/10, would learn again.
     
  16. karsnen

    karsnen

    Joined:
    Feb 9, 2014
    Posts:
    65
    Just to be clear,

    commenting Debug.Log statements would just be fine before release? Wouldn't it?
     
  17. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    There's a small performance hit every time you do a Debug.Log. You probably don't want that in your release other than instances when if you were to actually receive the log from a user that it provides helpful information.

    So, recording of errors for example.
     
    SparrowGS likes this.
  18. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Yes, that would essentially delete the line as far as the program is concerned.
    but as mentioned above, it can be useful to see what went wrong on the user end given a bug report so do use it in the build, but use it wisely.
     
  19. karsnen

    karsnen

    Joined:
    Feb 9, 2014
    Posts:
    65
    Thanks guys!
     
  20. PixelEnvision

    PixelEnvision

    Joined:
    Feb 7, 2012
    Posts:
    513
    I'm using this so there's no logging if the debug build is not checked.

    Code (CSharp):
    1.     void Awake()
    2.     {
    3.         //Disable logger if  not debug build
    4.         Debug.unityLogger.logEnabled = Debug.isDebugBuild;
    5.     }
     
    cdr9042, Lurking-Ninja and JoeStrout like this.