UnityLoggerBridge - jimdroberts/FishMMO GitHub Wiki

Description

Bridges Unity's logging system with the FishMMO.Logging system by implementing ILogHandler. Intercepts all Unity Debug.Log messages and forwards them to the central Log manager, while also preventing Unity's default console output. Designed to be separate from core ILogger implementations for flexible log routing and integration.


API Access

Fields

  • internal static bool IsLoggingInternally

    Internal flag to prevent re-capturing logs that the FishMMO logging system just wrote to Debug.Log. Set by loggers like UnityConsoleFormatter before they call Debug.Log.

  • private static UnityLoggerBridge instance

    Singleton instance of the bridge.

  • private ILogHandler defaultUnityLogHandler

    Stores Unity's original log handler for restoration.

  • private static Action _internalLogMessageCallback

    Callback for internal log messages.

Methods

  • public static void Initialize(Action internalLogMessageCallback)

    Initializes the Unity logger bridge and sets it as Unity's primary log handler. Should be called once, typically at application start. internalLogMessageCallback: Action - Callback for internal log messages.

  • public static void Shutdown()

    Shuts down the Unity logger bridge and restores Unity's default log handler. Should be called during application shutdown for cleanup.

  • public void LogFormat(LogType logType, UnityEngine.Object context, string format, params object[] args)

    Implementation of ILogHandler.LogFormat. Intercepts all Debug.Log, Debug.LogWarning, Debug.LogError messages. logType: LogType - The type of log message. context: UnityEngine.Object - The Unity object context. format: string - The log message format string. args: object[] - Arguments for the format string.

  • public void LogException(Exception exception, UnityEngine.Object context)

    Implementation of ILogHandler.LogException. Intercepts all Debug.LogException messages. exception: Exception - The exception to log. context: UnityEngine.Object - The Unity object context.

  • private static LogLevel ConvertLogTypeToLogLevel(LogType type)

    Converts Unity's LogType to FishMMO.Logging.LogLevel. type: LogType - The Unity LogType. Returns: LogLevel - The corresponding LogLevel.


Basic Usage

Setup

  1. Call UnityLoggerBridge.Initialize() at application startup to intercept Unity log messages.
  2. All Unity Debug.Log, Debug.LogWarning, Debug.LogError, and Debug.LogException messages will be routed to the FishMMO logging system.
  3. Call UnityLoggerBridge.Shutdown() during application shutdown to restore Unity's default log handler.
  4. Use the internal log message callback for diagnostics and bridge status updates.

Example

// Example 1: Initializing the UnityLoggerBridge
UnityLoggerBridge.Initialize(msg => Debug.Log(msg));

// Example 2: Shutting down the bridge on application exit
UnityLoggerBridge.Shutdown();

Best Practices

  • Always initialize the bridge at application start to ensure all Unity logs are captured.
  • Use the internal log message callback to monitor bridge status and initialization/shutdown events.
  • Restore Unity's default log handler on shutdown to avoid side effects in the editor or other tools.
  • Avoid direct use of Debug.Log in production code; use the FishMMO logging system for consistency.
  • Use the IsLoggingInternally flag to prevent log recursion and infinite loops.
⚠️ **GitHub.com Fallback** ⚠️