diff --git a/README.md b/README.md index 8046399..58d4586 100644 --- a/README.md +++ b/README.md @@ -65,8 +65,12 @@ Even if you do not use `Zenject` or a different DI container, you still can use ``` // The context can be any object and the type name will be used as filter, s.t. all logs from `class C` will end up in the Editor Console Pro Filter #C# // or it can be a string used as filter. +// public Logger(LogLevel logLevel = LogLevel.Trace) // public Logger(object context = null, LogLevel logLevel = LogLevel.Trace) -// +this.logger = new Logger(this, LogLevel.Debug); +this.logger = new Logger(LogLevel.Debug); +this.logger = new Logger(this); +this.logger = new Logger(); ``` #### Examples: Zenject Installation & Configuration @@ -82,7 +86,36 @@ Container.BindLogger(LogLevel.Debug); ``` #### Zenject Usage +``` +// Examples +[Inject, UsedImplicitly] +public void Inject(Logger.Factory logging, Logger logger) +{ + // Option 1: Inject Logger.Factory to create a logger with this context and a specific LogLevel + this.logger = logging.Create(this, LogLevel.Trace); + this.logger.Trace($"Option 1: {name}"); + // Option 2: Use the copy of the default logger and set the context manually + logger.SetContext(this); // Adds #this.GetType().Name# as filter for Editor Console Pro + logger.SetLogLevel(LogLevel.Debug); // Overwrite the LogLevel manually if you do not use Container.BindLogger(LogLevel) on DI level + logger.Trace($"Option 2: {name}"); + // Option 3: Use the static Logger with a) using Logger = Infrastructure.NoxLog.Logger; or b) specify the full namespace Infrastructure.NoxLog.Logger + Logger.StaticLogger.SetContext("Example"); // Optionally redefine the context + Logger.StaticLogger.SetLogLevel(LogLevel.Trace); // Optionally redefine the loglevel + Logger.LogTrace($"Option 3.1: Without context {name}"); + Logger.LogTrace(this, $"Option 3.2: With context {name}"); + // Option 4: use the extension method for the static logger. Requires LOGGER_EXTENSIONS + this.LogTrace("Inject 5"); + "Example".LogTrace("Inject 6"); // If this is a string, it will directly be picked up as a filter for Editor Console Pro, e.g. #Example# +} +``` +# Best Practices +* Do *not* use the static Logger.LogTrace(msg) or this.LogTrace(msg) or "Example".LogTraec(msg). + These are convenience functions to provide quick fully featured logging during tracer bullet style logging used commonly for debugging +* **Do** use constructor injection for the Logger. + * If you do not want to use any DI framework, inject the logger manually. This allows to inject a different logger during testing, e.g. one which does not log. + * If you do not want to inject the logger at all, at least create your logger per base class and inherit it. +* The usage of Editor Console Pro or any other console replacement for unity which allows temporary filters is **highly** recommended. It should be quite easy to change how the filter is appended in Logger.cs if you use a different console. # Supported Scripting Defines * `ENABLE_LOGGER` diff --git a/src/NoxLog/Logger.cs b/src/NoxLog/Logger.cs index 6c9bfed..8cac0e3 100644 --- a/src/NoxLog/Logger.cs +++ b/src/NoxLog/Logger.cs @@ -11,9 +11,9 @@ namespace Infrastructure.NoxLog { public class Logger { - internal static readonly Logger StaticLogger = new Logger(); - private string filter = ""; - private LogLevel logLevel; + public static readonly Logger StaticLogger = new Logger(); + private string filter = ""; + private LogLevel logLevel; public Logger(LogLevel logLevel = LogLevel.Trace) { @@ -40,7 +40,7 @@ public string GetFilter(object context) switch (context) { case null: return filter; - case string s: return s; + case string s: return $"#{s}#"; default: return context is Type t ? $"#{t.Name}#" : $"#{context.GetType().Name}#"; } } diff --git a/src/NoxLog/LoggerExtensions.cs b/src/NoxLog/LoggerExtensions.cs index c2a60e4..29c3296 100644 --- a/src/NoxLog/LoggerExtensions.cs +++ b/src/NoxLog/LoggerExtensions.cs @@ -3,11 +3,11 @@ namespace Infrastructure.NoxLog { public static class LoggerExtensions { - public static void LogTrace(this object context, string msg) => NoxLog.Logger.StaticLogger.Trace(msg, context); - public static void LogDebug(this object context, string msg) => NoxLog.Logger.StaticLogger.Debug(msg, context); - public static void LogInfo(this object context, string msg) => NoxLog.Logger.StaticLogger.Info(msg, context); - public static void LogWarning(this object context, string msg) => NoxLog.Logger.StaticLogger.Warning(msg, context); - public static void LogError(this object context, string msg) => NoxLog.Logger.StaticLogger.Error(msg, context); + public static void LogTrace(this object context, string msg) => Logger.StaticLogger.Trace(msg, context); + public static void LogDebug(this object context, string msg) => Logger.StaticLogger.Debug(msg, context); + public static void LogInfo(this object context, string msg) => Logger.StaticLogger.Info(msg, context); + public static void LogWarning(this object context, string msg) => Logger.StaticLogger.Warning(msg, context); + public static void LogError(this object context, string msg) => Logger.StaticLogger.Error(msg, context); } } #endif \ No newline at end of file diff --git a/src/NoxLog/version.txt b/src/NoxLog/version.txt index 1c96c20..0ce61f3 100644 --- a/src/NoxLog/version.txt +++ b/src/NoxLog/version.txt @@ -1 +1 @@ -0.0.2-alpha \ No newline at end of file +0.0.3-alpha \ No newline at end of file