Skip to content

Logging

Alexander edited this page Aug 25, 2022 · 3 revisions

Neon is using custom logging system, easy to override for your needs.

LogManager

It's a parent of loggers, it can have his own logging severity and meta information.

LogManager logManager = new LogManager(LogSeverity.DEBUG);
logManager.Handlers.Add(new LoggingHandlerConsole(new LoggingFormatterDefault()));

ILogger logger = logManager.GetLogger("TestLogger");
logger.Debug("test"); //[2022-08-25 15:56:23.348] [TestLogger] [DEBUG] test

You can set a logging severity for LogManager and Logger, but the message passes the filter only if the message severity equals or more than logger or log manager severity

Getting/setting a default (static) log manager:

LogManager.SetDefault(myLogManager);
ILogger logger = LogManager.Default.GetLogger(nameof(MyClass));

Formatters

Formatters help you format your logging message in any form you want. Neon.Logging has two built-in formatters: LoggingFormatterDefault and LoggingFormatterJson with few options:

  • IncludeTimestampInMessage - adds a datetime (default: true)
  • DateTimeFormat - datetime format (default: yyyy-MM-dd HH:mm:ss.fff)
  • IncludeLoggerNameInMessage - adds a logger name after datetime (default: true)
  • IncludeSeverityInMessage - adds a message severity

example of LoggingFormatterDefault output:

[2022-08-25 15:56:23.348] [TestLogger] [DEBUG] test

example of LoggingFormatterJson output:

{"severity": "INFO", "message": "[TestLogger] test", "timestamp": "2022-08-23T14:50:12.237255Z", "labels": {}, "loggerName": "TestLogger"}

Handlers

Handler provides an output destination for your messages. Neon.Logging has one built-in handler LoggingHandlerConsole it just redirects any output to the console

Unity

For Unity you may want to create custom handler like this:

public class UnityLoggingHandler : ILoggingHandler
{
    ILoggingFormatter formatter;

    public UnityLoggingHandler(ILoggingFormatter formatter)
    {
        this.formatter = formatter;
    }

    public void Write(LogSeverity severity, object message, LoggingMeta meta, ILogger logger)
    {
        string formatted = formatter.Format(severity, message, meta, logger);
        switch (severity)
        {
            case LogSeverity.TRACE:
            case LogSeverity.DEBUG:
            case LogSeverity.INFO:
                UnityEngine.Debug.Log(formatted);
                break;
            case LogSeverity.WARNING:
                UnityEngine.Debug.LogWarning(formatted);
                break;
            case LogSeverity.ERROR:
            case LogSeverity.CRITICAL:
                UnityEngine.Debug.LogError(formatted);
                break;
            default:
                break;
        }
    }
}

Meta

LogManager, Logger and a message may have a meta information, merged just before going to handler. In case there's two same keys the last one will be taken in order: LogManager, Logger, message Processing meta depends on the formatter and handler, for example LoggingFormatterJson append any meta information in the label field, but LoggingHandlerConsole drops meta

Clone this wiki locally