From 1aafb0b20155a9105505452cd15ddcde44ce2fde Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Wed, 31 Jul 2024 21:42:07 -0700 Subject: [PATCH] New: Add Compact Log Event Format option for console logging (cherry picked from commit 0d914f4c53876540ed2df83ad3d71615c013856f) --- .../Instrumentation/NzbDroneLogger.cs | 20 ++++++++++++++++++- src/NzbDrone.Common/Options/LogOptions.cs | 1 + src/NzbDrone.Common/Prowlarr.Common.csproj | 1 + .../Configuration/ConfigFileProvider.cs | 7 +++++++ .../Instrumentation/ReconfigureLogging.cs | 20 ++++++++++++++++++- 5 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/NzbDrone.Common/Instrumentation/NzbDroneLogger.cs b/src/NzbDrone.Common/Instrumentation/NzbDroneLogger.cs index 9351256d7..11836b2af 100644 --- a/src/NzbDrone.Common/Instrumentation/NzbDroneLogger.cs +++ b/src/NzbDrone.Common/Instrumentation/NzbDroneLogger.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.IO; using NLog; using NLog.Config; +using NLog.Layouts.ClefJsonLayout; using NLog.Targets; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Extensions; @@ -13,6 +14,8 @@ 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 static bool _isConfigured; @@ -111,7 +114,16 @@ namespace NzbDrone.Common.Instrumentation var coloredConsoleTarget = new ColoredConsoleTarget(); coloredConsoleTarget.Name = "consoleLogger"; - coloredConsoleTarget.Layout = "[${level}] ${logger}: ${message} ${onexception:inner=${newline}${newline}[v${assembly-version}] ${exception:format=ToString}${newline}${exception:format=Data}${newline}}"; + + var logFormat = Enum.TryParse(Environment.GetEnvironmentVariable("PROWLARR__LOG__CONSOLEFORMAT"), out var formatEnumValue) + ? formatEnumValue + : ConsoleLogFormat.Standard; + + coloredConsoleTarget.Layout = logFormat switch + { + ConsoleLogFormat.Clef => ClefLogLayout, + _ => ConsoleLogLayout + }; var loggingRule = new LoggingRule("*", level, coloredConsoleTarget); @@ -206,4 +218,10 @@ namespace NzbDrone.Common.Instrumentation return GetLogger(obj.GetType()); } } + + public enum ConsoleLogFormat + { + Standard, + Clef + } } diff --git a/src/NzbDrone.Common/Options/LogOptions.cs b/src/NzbDrone.Common/Options/LogOptions.cs index 1529bb1d0..4110f6654 100644 --- a/src/NzbDrone.Common/Options/LogOptions.cs +++ b/src/NzbDrone.Common/Options/LogOptions.cs @@ -7,6 +7,7 @@ public class LogOptions public int? Rotate { get; set; } public bool? Sql { get; set; } public string ConsoleLevel { get; set; } + public string ConsoleFormat { get; set; } public bool? AnalyticsEnabled { get; set; } public string SyslogServer { get; set; } public int? SyslogPort { get; set; } diff --git a/src/NzbDrone.Common/Prowlarr.Common.csproj b/src/NzbDrone.Common/Prowlarr.Common.csproj index e6f6e6cdc..fd68fa9e7 100644 --- a/src/NzbDrone.Common/Prowlarr.Common.csproj +++ b/src/NzbDrone.Common/Prowlarr.Common.csproj @@ -9,6 +9,7 @@ + diff --git a/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs b/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs index aa6509ef4..b4959526f 100644 --- a/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs +++ b/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs @@ -10,6 +10,7 @@ using NzbDrone.Common.Cache; using NzbDrone.Common.Disk; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Extensions; +using NzbDrone.Common.Instrumentation; using NzbDrone.Common.Options; using NzbDrone.Core.Authentication; using NzbDrone.Core.Configuration.Events; @@ -38,6 +39,7 @@ namespace NzbDrone.Core.Configuration bool AnalyticsEnabled { get; } string LogLevel { get; } string ConsoleLogLevel { get; } + ConsoleLogFormat ConsoleLogFormat { get; } bool LogSql { get; } int LogRotate { get; } bool FilterSentryEvents { get; } @@ -225,6 +227,11 @@ namespace NzbDrone.Core.Configuration public string LogLevel => _logOptions.Level ?? GetValue("LogLevel", "debug").ToLowerInvariant(); public string ConsoleLogLevel => _logOptions.ConsoleLevel ?? GetValue("ConsoleLogLevel", string.Empty, persist: false); + public ConsoleLogFormat ConsoleLogFormat => + Enum.TryParse(_logOptions.ConsoleFormat, out var enumValue) + ? enumValue + : GetValueEnum("ConsoleLogFormat", ConsoleLogFormat.Standard, false); + public string Theme => _appOptions.Theme ?? GetValue("Theme", "auto", persist: false); public string PostgresHost => _postgresOptions?.Host ?? GetValue("PostgresHost", string.Empty, persist: false); diff --git a/src/NzbDrone.Core/Instrumentation/ReconfigureLogging.cs b/src/NzbDrone.Core/Instrumentation/ReconfigureLogging.cs index 4e5cfbead..d9a1f3397 100644 --- a/src/NzbDrone.Core/Instrumentation/ReconfigureLogging.cs +++ b/src/NzbDrone.Core/Instrumentation/ReconfigureLogging.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using NLog; using NLog.Config; +using NLog.Targets; using NLog.Targets.Syslog; using NLog.Targets.Syslog.Settings; using NzbDrone.Common.EnvironmentInfo; @@ -50,7 +51,8 @@ namespace NzbDrone.Core.Instrumentation var rules = LogManager.Configuration.LoggingRules; - //Console + // Console + ReconfigureConsole(); SetMinimumLogLevel(rules, "consoleLogger", minimumConsoleLogLevel); //Log Files @@ -109,6 +111,22 @@ namespace NzbDrone.Core.Instrumentation } } + private void ReconfigureConsole() + { + var consoleTarget = LogManager.Configuration.AllTargets.OfType().FirstOrDefault(); + + if (consoleTarget != null) + { + var format = _configFileProvider.ConsoleLogFormat; + + consoleTarget.Layout = format switch + { + ConsoleLogFormat.Clef => NzbDroneLogger.ClefLogLayout, + _ => NzbDroneLogger.ConsoleLogLayout + }; + } + } + private void SetSyslogParameters(string syslogServer, int syslogPort, LogLevel minimumLogLevel) { var syslogTarget = new SyslogTarget();