Pimp My Debug.Log

This post describes a quick “tip” for improving how you work with logging in Unity. Share it with your team in case you find it useful 🙂

Console Window Mess

As developers we probably spend 50% of our time debugging code (it is my blog, so i get make up fake statistics). Any Unity developer who spends more than 5 minutes with the editor probably knows the Debug class, and more specifically the Debug.Log method that prints log messages to the Console window.

While it’s easy to log messages, the Console window can quickly get cluttered and it becomes super hard to find the exact trace message you’re looking for.

Rich text formatting seems to be one of Unity’s best kept secrets (haven’t seen people using it yet), but it can come in handy for tidying up the Console window.

Rich Text Formatting

Log messages passed to Debug.Log and its other variants (LogError, LogWarning, etc) can include special “tags” that provide formatting for the log message, for example:

Debug.Log("<color=green>This is a green message!</color>");

These tags control the visual properties of the displayed text, such as its size and color. For all the fine details check out the documentation here.

The only issue is that it can be tedious to manually format all log messages, which may drive you to not use this feature at all.

Luckily, there’s a better option. Enter the “Extension Method” solution.

String Extensions Class

Instead of manually formatting every log message and adding the required tags, we can create a static extensions class, and define a few extension methods on the string type.

The following code snippet adds a few extension methods on the string class:

This allows writing simple code, without having to manually add the formatting tags:

// Update is called once per frame
void Update ()
    var message = "Hello, World!";
    // Print the message in green.
    Debug.Log (message.Colored (Colors.green));

    // Compose different formatting together.
    Debug.Log (message.Bold().Sized(16));

    // Works with LogError as well.
    Debug.LogError (message.Italics());

The code snippet above prints out the following logs in the Console window, with the proper formatting:

Console window output

Console window output


  • Rich text formatting works with all Debug.LogXXX methods.
  • Editor GUI controls also support rich formatting, however it must be explicitly enabled.
GUIStyle style = new GUIStyle ();
style.richText = true;
This entry was posted in GameDev, Unity and tagged , , , . Bookmark the permalink.