1
0
Fork 0
mirror of https://github.com/lidarr/Lidarr synced 2024-12-21 23:32:27 +00:00

New: Add Compact Log Event Format option for console logging

(cherry picked from commit 0d914f4c53876540ed2df83ad3d71615c013856f)

Closes #4981
This commit is contained in:
Mark McDowall 2024-07-31 21:42:07 -07:00 committed by Bogdan
parent bb6528c104
commit 828e04bcad
5 changed files with 46 additions and 1 deletions

View file

@ -3,6 +3,7 @@
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;
@ -110,7 +113,16 @@ private static void RegisterConsole()
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<ConsoleLogFormat>(Environment.GetEnvironmentVariable("LIDARR__LOG__CONSOLEFORMAT"), out var formatEnumValue)
? formatEnumValue
: ConsoleLogFormat.Standard;
coloredConsoleTarget.Layout = logFormat switch
{
ConsoleLogFormat.Clef => ClefLogLayout,
_ => ConsoleLogLayout
};
var loggingRule = new LoggingRule("*", level, coloredConsoleTarget);
@ -205,4 +217,10 @@ public static Logger GetLogger(object obj)
return GetLogger(obj.GetType());
}
}
public enum ConsoleLogFormat
{
Standard,
Clef
}
}

View file

@ -11,6 +11,7 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.3.3" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.2" />
<PackageReference Include="NLog.Layouts.ClefJsonLayout" Version="1.0.0" />
<PackageReference Include="Sentry" Version="3.25.0" />
<PackageReference Include="SharpZipLib" Version="1.4.2" />
<PackageReference Include="System.IO.Abstractions" Version="17.0.24" />

View file

@ -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; }

View file

@ -9,6 +9,7 @@
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 @@ public interface IConfigFileProvider : IHandleAsync<ApplicationStartedEvent>,
bool AnalyticsEnabled { get; }
string LogLevel { get; }
string ConsoleLogLevel { get; }
ConsoleLogFormat ConsoleLogFormat { get; }
bool LogSql { get; }
int LogRotate { get; }
bool FilterSentryEvents { get; }
@ -221,6 +223,11 @@ public AuthenticationType AuthenticationMethod
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<ConsoleLogFormat>(_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);

View file

@ -2,6 +2,7 @@
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 @@ public void Reconfigure()
var rules = LogManager.Configuration.LoggingRules;
// Console
ReconfigureConsole();
SetMinimumLogLevel(rules, "consoleLogger", minimumConsoleLogLevel);
// Log Files
@ -109,6 +111,22 @@ private void ReconfigureSentry()
}
}
private void ReconfigureConsole()
{
var consoleTarget = LogManager.Configuration.AllTargets.OfType<ColoredConsoleTarget>().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();