Skip to content

Unity: Debug.Log console messages with timestamp

February 1, 2014

While Unity is packed with professionalism all over, the one thing that is astonishingly basic is the output of debug messages to the console log. Until these days there is really not much you can configure or customize here. It is like it is. And that looks like that:

UnityConsoleWithoutTimestamp

What I really missed is at least a proper timestamp. For one it prevents the aggregation of identical message (I never understood what that would be good for, but maybe that’s just me…), furthermore you can check how current the latest entry in your log is. I think it is not even necessary to explain why that is useful. Every other log in the world would have timestamps for each entry, so we want our log to look like this:

UnityConsoleWithTimestamp

Actually this is pretty easy and not much more than a bit of routine work. Since I did it already, you don’t have to. Unitys Debug class is declared as sealed, so we can’t just inherit from it and overwrite what we need. Instead we just define a new class named Debug. It will cover the original one  and call ours (as long as someone didn’t fully qualify a call with the UnityEngine namespace, which you would normally not do anyway). We just prefix it with the desired format of a timestamp (the code contains already one alternative for you to play with) and call the original log function. And done.

But just wait: since your new Debug class will catch all functions of the original class, you have to do a bit more tedious work to mirror all of them and route them to the Unity implementation, if it should work seamlessly. Lucky you…I did that as well. So just download the C# class and drop it into your script directory, and you are instantly enjoying timestamped log messages without any additional work.

Advertisements

From → Development

5 Comments
  1. Waltari permalink

    Hey, I tried implementing this, but I am encountering some issues. Does this still work with Unity 5? Basically when I throw this into the script folder with my other scripts and use the Debug func it doesn’t give timestamps. Just does what it has always done.

    • I haven’t actually switched to Unity 5 yet, but I would assume that for compatibility reasons the Debug class still exists, and as such should also be well overridable. I don’t see any reason why this would not work. But I can’t tell for sure yet…

  2. James permalink

    Also not working for me. I know it’s being called because my asserts stopped working (Debug.Assert) because your debug doesn’t have it. But as Waltari said it doesn’t work; no timestamp shows and the message looks identical to the normal debug.

    • Ok, so now I got curious 😉
      Just installed Unity 5 and tried myself, but it still works exactly as expected. Since there was no change in the Debug.xxx functions, this is actually what I would have expected. It’s really no rocket science…just overloading the existing functions. I am not sure, though, whether some other side effects in your projects would make Unity lookup the Debug.xxx functions in the UnityEngine namespace first, but usually this should not happen. Can you breakpoint in the override functions? Or does the stack of the log output show these two lines first:

      11:29:21.125: Testlog <==== This is the actual log output, including timestamp
      UnityEngine.Debug:Log(Object) <==== This is the normal log fct., being called with preceding timestamp
      Debug:Log(Object, Object) (at Assets/<path to your scripts>/Debug.cs:51) <==== This is the override code
      ...

      In this case maybe just the timestamp generation fails for you. Please try to return some “fooBar” teststring form the getTimestamp() function and see whether it’s put in front of the log output instead now.

  3. work like a charm here , well done man !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: