Skip to content

A custom logger for Unity with custom categories and logs only available on Editor and Development Builds.

License

Notifications You must be signed in to change notification settings

JoanStinson/UnityLoggerExtended

Repository files navigation

Unity Logger Extended

A custom logger for Unity with custom categories and logs only available on Editor and Development Builds.

Made With Unity License Last Commit Repo Size Downloads Last Release

🖥️ How It Works

For starters, import the package located in the Releases section into your project.

Now then, there's a Console class, which basically serves as a wrapper of Unity's Debug class for logs. Each log type (info, warning, error) has it's own custom color and you can decide to define a category for each one (which will appear in bold in front).

Furthermore, this system lets you define custom categories on the fly, with no need to maintain any Enums or Scriptable Objects. Just pass in the category before the log message and whoala!

Example Usage:

private void Awake()
{
    Console.Log("Hello World!");
    Console.LogFormat("Hell{0} W{1}rld!", 0, 0);
    Console.Log("UI", "Hello World!");
    Console.LogFormat("UI", "Hell{0} W{1}rld!", 0, 0);

    Console.LogWarning("Hello World!");
    Console.LogWarningFormat("Hell{0} W{1}rld!", 0, 0);
    Console.LogWarning("Audio", "Hello World!");
    Console.LogWarningFormat("Audio", "Hell{0} W{1}rld!", 0, 0);

    Console.LogError("Hello World!");
    Console.LogErrorFormat("Hell{0} W{1}rld!", 0, 0);
    Console.LogError("Physics", "Hello World!");
    Console.LogErrorFormat("Physics", "Hell{0} W{1}rld!", 0, 0);

    Console.LogException(new NullReferenceException());
    Console.LogException("Analytics", new NullReferenceException());
}

The best approach for me, would be to have a static class with all your custom category const strings (to avoid harcoded strings!).

Output:

Fast Log Filtering:

📵 How To Disable Logs on Release Builds

To disable Unity Logs on Release Builds and have them be available only on the Editor and Development Builds, you can do it this way:

    private void Awake()
    {
#if DEBUG
        Debug.unityLogger.logEnabled = true;
#else
        Debug.unityLogger.logEnabled = false;
#endif
    }

By using Unity Logger Extended, this is automatically achieved via the Conditional Attributes located on top of each method:

public static class Console
{
    [Conditional("DEBUG")]
    public static void Log(object message)
    {
        Debug.Log(message);
    }
}

Like this, none of your log calls will be executed without having to remove any code! I know, super useful right?

Alternatively, you can undefine the DEBUG symbol manually, by doing so:

#undef DEBUG