diff --git a/src/NzbDrone.Common/Instrumentation/CleansingClefLogLayout.cs b/src/NzbDrone.Common/Instrumentation/CleansingClefLogLayout.cs new file mode 100644 index 000000000..f110b96ac --- /dev/null +++ b/src/NzbDrone.Common/Instrumentation/CleansingClefLogLayout.cs @@ -0,0 +1,21 @@ +using System.Text; +using NLog; +using NLog.Layouts.ClefJsonLayout; +using NzbDrone.Common.EnvironmentInfo; + +namespace NzbDrone.Common.Instrumentation; + +public class CleansingClefLogLayout : CompactJsonLayout +{ + protected override void RenderFormattedMessage(LogEventInfo logEvent, StringBuilder target) + { + base.RenderFormattedMessage(logEvent, target); + + if (RuntimeInfo.IsProduction) + { + var result = CleanseLogMessage.Cleanse(target.ToString()); + target.Clear(); + target.Append(result); + } + } +} diff --git a/src/NzbDrone.Common/Instrumentation/CleansingConsoleLogLayout.cs b/src/NzbDrone.Common/Instrumentation/CleansingConsoleLogLayout.cs new file mode 100644 index 000000000..f894a4df5 --- /dev/null +++ b/src/NzbDrone.Common/Instrumentation/CleansingConsoleLogLayout.cs @@ -0,0 +1,26 @@ +using System.Text; +using NLog; +using NLog.Layouts; +using NzbDrone.Common.EnvironmentInfo; + +namespace NzbDrone.Common.Instrumentation; + +public class CleansingConsoleLogLayout : SimpleLayout +{ + public CleansingConsoleLogLayout(string format) + : base(format) + { + } + + protected override void RenderFormattedMessage(LogEventInfo logEvent, StringBuilder target) + { + base.RenderFormattedMessage(logEvent, target); + + if (RuntimeInfo.IsProduction) + { + var result = CleanseLogMessage.Cleanse(target.ToString()); + target.Clear(); + target.Append(result); + } + } +} diff --git a/src/NzbDrone.Common/Instrumentation/NzbDroneFileTarget.cs b/src/NzbDrone.Common/Instrumentation/CleansingFileTarget.cs similarity index 87% rename from src/NzbDrone.Common/Instrumentation/NzbDroneFileTarget.cs rename to src/NzbDrone.Common/Instrumentation/CleansingFileTarget.cs index 84658cf74..f74d1fca4 100644 --- a/src/NzbDrone.Common/Instrumentation/NzbDroneFileTarget.cs +++ b/src/NzbDrone.Common/Instrumentation/CleansingFileTarget.cs @@ -4,7 +4,7 @@ namespace NzbDrone.Common.Instrumentation { - public class NzbDroneFileTarget : FileTarget + public class CleansingFileTarget : FileTarget { protected override void RenderFormattedMessage(LogEventInfo logEvent, StringBuilder target) { diff --git a/src/NzbDrone.Common/Instrumentation/NzbDroneLogger.cs b/src/NzbDrone.Common/Instrumentation/NzbDroneLogger.cs index dce90afbf..69b791a4d 100644 --- a/src/NzbDrone.Common/Instrumentation/NzbDroneLogger.cs +++ b/src/NzbDrone.Common/Instrumentation/NzbDroneLogger.cs @@ -3,7 +3,6 @@ using System.IO; using NLog; using NLog.Config; -using NLog.Layouts.ClefJsonLayout; using NLog.Targets; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Extensions; @@ -13,9 +12,11 @@ namespace NzbDrone.Common.Instrumentation { public static class NzbDroneLogger { - private const string FILE_LOG_LAYOUT = @"${date:format=yyyy-MM-dd HH\:mm\:ss.f}|${level}|${logger}|${message}${onexception:inner=${newline}${newline}[v${assembly-version}] ${exception:format=ToString}${newline}${exception:format=Data}${newline}}"; - public const string ConsoleLogLayout = "[${level}] ${logger}: ${message} ${onexception:inner=${newline}${newline}[v${assembly-version}] ${exception:format=ToString}${newline}${exception:format=Data}${newline}}"; - public static CompactJsonLayout ClefLogLayout = new CompactJsonLayout(); + private const string FileLogLayout = @"${date:format=yyyy-MM-dd HH\:mm\:ss.f}|${level}|${logger}|${message}${onexception:inner=${newline}${newline}[v${assembly-version}] ${exception:format=ToString}${newline}${exception:format=Data}${newline}}"; + private const string ConsoleFormat = "[${level}] ${logger}: ${message} ${onexception:inner=${newline}${newline}[v${assembly-version}] ${exception:format=ToString}${newline}${exception:format=Data}${newline}}"; + + private static readonly CleansingConsoleLogLayout CleansingConsoleLayout = new (ConsoleFormat); + private static readonly CleansingClefLogLayout ClefLogLayout = new (); private static bool _isConfigured; @@ -119,11 +120,7 @@ private static void RegisterConsole() ? formatEnumValue : ConsoleLogFormat.Standard; - coloredConsoleTarget.Layout = logFormat switch - { - ConsoleLogFormat.Clef => ClefLogLayout, - _ => ConsoleLogLayout - }; + ConfigureConsoleLayout(coloredConsoleTarget, logFormat); var loggingRule = new LoggingRule("*", level, coloredConsoleTarget); @@ -140,7 +137,7 @@ private static void RegisterAppFile(IAppFolderInfo appFolderInfo) private static void RegisterAppFile(IAppFolderInfo appFolderInfo, string name, string fileName, int maxArchiveFiles, LogLevel minLogLevel) { - var fileTarget = new NzbDroneFileTarget(); + var fileTarget = new CleansingFileTarget(); fileTarget.Name = name; fileTarget.FileName = Path.Combine(appFolderInfo.GetLogFolder(), fileName); @@ -153,7 +150,7 @@ private static void RegisterAppFile(IAppFolderInfo appFolderInfo, string name, s fileTarget.MaxArchiveFiles = maxArchiveFiles; fileTarget.EnableFileDelete = true; fileTarget.ArchiveNumbering = ArchiveNumberingMode.Rolling; - fileTarget.Layout = FILE_LOG_LAYOUT; + fileTarget.Layout = FileLogLayout; var loggingRule = new LoggingRule("*", minLogLevel, fileTarget); @@ -172,7 +169,7 @@ private static void RegisterUpdateFile(IAppFolderInfo appFolderInfo) fileTarget.ConcurrentWrites = false; fileTarget.ConcurrentWriteAttemptDelay = 50; fileTarget.ConcurrentWriteAttempts = 100; - fileTarget.Layout = FILE_LOG_LAYOUT; + fileTarget.Layout = FileLogLayout; var loggingRule = new LoggingRule("*", LogLevel.Trace, fileTarget); @@ -217,6 +214,15 @@ public static Logger GetLogger(object obj) { return GetLogger(obj.GetType()); } + + public static void ConfigureConsoleLayout(ColoredConsoleTarget target, ConsoleLogFormat format) + { + target.Layout = format switch + { + ConsoleLogFormat.Clef => NzbDroneLogger.ClefLogLayout, + _ => NzbDroneLogger.CleansingConsoleLayout + }; + } } public enum ConsoleLogFormat diff --git a/src/NzbDrone.Core/Instrumentation/ReconfigureLogging.cs b/src/NzbDrone.Core/Instrumentation/ReconfigureLogging.cs index 1310255fd..d49bb06d7 100644 --- a/src/NzbDrone.Core/Instrumentation/ReconfigureLogging.cs +++ b/src/NzbDrone.Core/Instrumentation/ReconfigureLogging.cs @@ -95,7 +95,7 @@ private void SetMinimumLogLevel(LoggingRule rule, LogLevel minimumLogLevel) private void ReconfigureFile() { - foreach (var target in LogManager.Configuration.AllTargets.OfType()) + foreach (var target in LogManager.Configuration.AllTargets.OfType()) { target.MaxArchiveFiles = _configFileProvider.LogRotate; target.ArchiveAboveSize = _configFileProvider.LogSizeLimit.Megabytes(); @@ -120,11 +120,7 @@ private void ReconfigureConsole() { var format = _configFileProvider.ConsoleLogFormat; - consoleTarget.Layout = format switch - { - ConsoleLogFormat.Clef => NzbDroneLogger.ClefLogLayout, - _ => NzbDroneLogger.ConsoleLogLayout - }; + NzbDroneLogger.ConfigureConsoleLayout(consoleTarget, format); } }