Skip to content

Instantly share code, notes, and snippets.

@athiedmann
Created October 3, 2023 22:03
Show Gist options
  • Select an option

  • Save athiedmann/8e2b768245841fb60229f1ed7ed8a036 to your computer and use it in GitHub Desktop.

Select an option

Save athiedmann/8e2b768245841fb60229f1ed7ed8a036 to your computer and use it in GitHub Desktop.
This is liortal's script, which serves as a customizable logging utility for Unity projects. It wraps Unity's internal logger functions (e.g., Debug.Log, Debug.LogWarning, Debug.LogError) and allows you to enable or disable logging at compile time based on a defined symbol. Original post -> https://tinyurl.com/4un88vyn
using UnityEngine;
////////////////////////////////////////////////////////////////////////
//
// From liortal's script that was posted in the Unity forum
//
////////////////////////////////////////////////////////////////////////
/// <summary>
/// A logger that wraps Unity's internal logger.
/// Calls to its methods are stripped in case the LOGGER_SYMBOL is not defined.
/// In Project Settings under Scripting Define Symbols, add ENABLE_LOG. Remove when building.
/// </summary>
public sealed class Logger
{
public const string LOGGER_SYMBOL = "ENABLE_LOG";
[System.Diagnostics.Conditional(LOGGER_SYMBOL)]
public static void Log(object message)
{
Debug.Log(message);
}
[System.Diagnostics.Conditional(LOGGER_SYMBOL)]
public static void Log(object message, Object context)
{
Debug.Log(message, context);
}
[System.Diagnostics.Conditional(LOGGER_SYMBOL)]
public static void LogFormat(string message, params object[] args)
{
Debug.LogFormat(message, args);
}
[System.Diagnostics.Conditional(LOGGER_SYMBOL)]
public static void LogFormat(Object context, string message, params object[] args)
{
Debug.LogFormat(context, message, args);
}
[System.Diagnostics.Conditional(LOGGER_SYMBOL)]
public static void LogWarning(object message)
{
Debug.LogWarning(message);
}
[System.Diagnostics.Conditional(LOGGER_SYMBOL)]
public static void LogWarning(object message, Object context)
{
Debug.LogWarning(message, context);
}
[System.Diagnostics.Conditional(LOGGER_SYMBOL)]
public static void LogWarningFormat(string message, params object[] args)
{
Debug.LogWarningFormat(message, args);
}
[System.Diagnostics.Conditional(LOGGER_SYMBOL)]
public static void LogWarningFormat(Object context, string message, params object[] args)
{
Debug.LogWarningFormat(context, message, args);
}
[System.Diagnostics.Conditional(LOGGER_SYMBOL)]
public static void LogError(object message)
{
Debug.LogError(message);
}
[System.Diagnostics.Conditional(LOGGER_SYMBOL)]
public static void LogError(object message, Object context)
{
Debug.LogError(message, context);
}
[System.Diagnostics.Conditional(LOGGER_SYMBOL)]
public static void LogErrorFormat(string message, params object[] args)
{
Debug.LogErrorFormat(message, args);
}
[System.Diagnostics.Conditional(LOGGER_SYMBOL)]
public static void LogErrorFormat(Object context, string message, params object[] args)
{
Debug.LogErrorFormat(context, message, args);
}
[System.Diagnostics.Conditional(LOGGER_SYMBOL)]
public static void LogException(System.Exception exception)
{
Debug.LogException(exception);
}
[System.Diagnostics.Conditional(LOGGER_SYMBOL)]
public static void LogException(System.Exception exception, Object context)
{
Debug.LogException(exception, context);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment