- Home /
Debug.Log(); or print();? What's the difference and when to use what?
Debug.Log();
print(); or even
System.Console.WriteLine(); (which only writes to the Editor-Logfile, not to the console),
or maybe others I may not be aware of...
Are there any rules as to when to use what?
Any reason for the (seeming) redundancy?
I found this related question with some very good answers:
http://answers.unity3d.com/questions/650/debug-log-doesnt-work-properly-in-a-contextmenu-function
To quote Jashans answer:
[...] the reason is very likely that log statements are somehow batched for performance reasons (e.g. only putting out logging "once per frame" which would mean all log-statements from one frame are sent to the console in a single step [...]
Can anyone comment on that?
Are there any other pitfalls to be aware of?
Like one of these not producing any output under particular circumstances or something?
Thanks & Greetz, Ky.
Answer by duck · Jan 14, 2010 at 12:42 PM
There is no functional difference between Debug.Log() and print(). print() simply wraps the Debug.Log command, and is in effect just an easier-to-remember alias to the same command. Because it's a function on the MonoBehaviour class, it means that it does not need to be preceded by an object reference when writing monobehaviours.
Whether or not Debug.Log messages are batched in builds to improve performance (I don't know about that), it's best to completely remove them once you're happy with your final build - particularly any which are printed during you game update functions!
In flash there's a publish option to NOOP all trace() calls, rather than manually commenting them out. Is there something like that in Unity?
I don't think so. However to implement this you could write your own function which wraps the Debug.Log function, with an addition 'enabled' boolean which you could switch on/off.
You could also create a new Log() method that is marked with the ConditionalAttribute("DEBUG"). The compiler will remove all calls to this method in case DEBUG symbol is not defined.
Unity since has had a "Use player log" option, so you can simply turn that off.
Answer by Santokes · Dec 21, 2011 at 06:20 AM
I'm surprised no one mentioned this.
print() only works if your class inherits from MonoBehavior.
If it doesn't your only choice is to use Debug.Log().
I feel this should be the accepted answer actually. It is also worth mentioning that Debug.Log() is capable of taking 2 parameters while print() only takes one. The second parameter in the Debug.Log works kind of like .Assert as it acts like a condition is null. Well, also Debug
is a class itself that was specifically designed and implemented to help developers with debugging code. It provides more static functions that help catching bugs. The poor print seems to be only working with classes derived from $$anonymous$$onoBehaviour. It seems it has been implemented long time ago and Debug class has replaced it extending the functionality and possibilities.
It was mentioned here: "Because it's a function on the $$anonymous$$onoBehaviour class, ..."
If your class does not inherit from $$anonymous$$onoBehavior, you can also use:
$$anonymous$$onoBehaviour.print()
It will behave like Debug.Log().
Answer by jpsmarinho812 · Jul 31, 2014 at 08:47 PM
On the reference page of the class Monobehaviour, is write that the print function is identical do Debug.Log:
BlockquoteLogs message to the Unity Console (identical to Debug.Log).
Except it's not, as Santakes' answer indicates, and mehow's comment below that elaborates on.
Answer by dabalciunas · Jun 23, 2019 at 07:52 PM
If you examine UnityEngine.CoreModule.dll with ILSpy, you'll notice that MonoBehaviour.print(object message) calls Debug.Log(object message).