6

My colleagues say that remaining Debug.Log causes latency. But I think it is useful for enhancing and accelerating our development. Furthermore, I found there is an option that disables Debug.Log on the production environment.

Give me your opinions.

Yoosam Kang
  • 61
  • 1
  • 4
  • 1
    Debug.Log Implies that one the release build those calls will be removed using if defs but you should check – johnny 5 Mar 16 '17 at 15:13

2 Answers2

9

When a Debug.Log() is called, it can affect performance because it will do some stack trace parsing and then write some stuff to a file.

It has to dig right down into the stack trace so if you have a lot of Debug.Logs then you might see a performance hit in your build.

You might want to circumvent this by only calling your Debug.Log statements when in Debug Build. From the Unity Docs:

In the Build Settings dialog there is a check box called "Development Build".

So you could do something like this that will only be called if the box is checked:

if (Debug.isDebugBuild) {
     Debug.Log ("If you can see this, you are in Debug Mode");
}

An easier alternative is to go to Player Settings and un-check "Use Player Log": enter image description here

This will stop Log files being written.

  • Wow, I never expected that print() is a wrapper for Debug.Log that extracts stacktrace. I was surprised when studying the profiler output and seeing all the extract stck calls. – DataGreed Jan 31 '19 at 19:18
5

If you have couple of logs every frame then yes, it will decrease your performance. But using it smart will only give you very important feedback for what is going inside your code. It also help to detect, e.g not working some web services or anything you can't predict. MY advice is use logging, but do it smart

What I recommend you is to write your own logger class. You can then disable/enable logging for particular types/scenarios/environments/whatever. Also I prefer to add location of the log on the begging, like [ClassName.MethodName()] LOG-MESSAGE Creating your own logger class will give you many benefits while it shouldn't take you much time. I personally made custom logger class for myself which can disable logs for particular types (so you can just turn on/off debug when needed without changing code, just one flag) and formatting the log message that it looks nicer. It uses Debug.Log on the end anyways. It's compiled to separated assembly so if you click the log in Unity console it still redirects you to the line of code where log was triggered. If you won't compile it to the assembly it will redirect you to your custom logger method that triggers Debug.Log.

It's only my advice of using custom logger, but generally I highly recommend using logging. Couple of logs triggered when user done something interesting won't affect your app, really.

piotrmitrega
  • 308
  • 1
  • 6
  • 1
    Just a little question, how do you manage to get the class and method name? I've tried [diferent](http://stackoverflow.com/questions/2113069/c-sharp-getting-its-own-class-name) aproaches in c# but it doesn't seems to work and [others](http://answers.unity3d.com/questions/126315/debuglog-in-build.html) looks too complicated for what I want. The most I can Log is UnityObjectName + message. This is a trouble because I've a static method which are debuging but, as you know, the double click in Unity shows me my method instead of the class and method that call it. – Salvador Lemus Mar 16 '17 at 20:08
  • 1
    Like this: string stackTrace = UnityEngine.StackTraceUtility.ExtractStackTrace(); string[] array = stackTrace.Split(new string[] {"\n"}, StringSplitOptions.None); Then, in array you have every stack trace frame separated. Just try it! Result would be like this: [Method callind Debug.log] [Previous Method] [Previous Previous Method] etc... If you compile your logger class to separated assembly (just create separated Unity project and google how to produce assembly. Let me know if you have problem with that) after double click it will behave as desired – piotrmitrega Mar 16 '17 at 21:28
  • Hello! I just try your solution and it seems that it isn't what I was looking for or I just did somethig wrong. Any way, your code shows me the stack for the Log proces in Unity but since all the logs comes from my Log implementation the stack shows me my own script, I don't know if I did somethig wrong but thanks for your help and I'm goin to continue with my research. – Salvador Lemus Mar 17 '17 at 19:10