From ace692aca6b49826de474b6ad990e5e92ed01f43 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) Closes #10266 --- .../Instrumentation/NzbDroneLogger.cs | 20 ++++++++++++++++++- src/NzbDrone.Common/Options/LogOptions.cs | 1 + src/NzbDrone.Common/Radarr.Common.csproj | 1 + .../Configuration/ConfigFileProvider.cs | 8 ++++++++ .../Instrumentation/ReconfigureLogging.cs | 18 +++++++++++++++++ 5 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/NzbDrone.Common/Instrumentation/NzbDroneLogger.cs b/src/NzbDrone.Common/Instrumentation/NzbDroneLogger.cs index 9f1f6ed9d..717ce4ff4 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("RADARR__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/Radarr.Common.csproj b/src/NzbDrone.Common/Radarr.Common.csproj index ea19653f4..8906b53ab 100644 --- a/src/NzbDrone.Common/Radarr.Common.csproj +++ b/src/NzbDrone.Common/Radarr.Common.csproj @@ -9,6 +9,7 @@ + diff --git a/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs b/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs index ff1f32b5f..96b226e96 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; @@ -39,6 +40,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; } @@ -223,6 +225,12 @@ 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); public string PostgresUser => _postgresOptions?.User ?? GetValue("PostgresUser", string.Empty, persist: false); diff --git a/src/NzbDrone.Core/Instrumentation/ReconfigureLogging.cs b/src/NzbDrone.Core/Instrumentation/ReconfigureLogging.cs index 08cb4ca60..5785662d9 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; @@ -51,6 +52,7 @@ namespace NzbDrone.Core.Instrumentation var rules = LogManager.Configuration.LoggingRules; // 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();