From 8732f30563ad340d7fca8f9ded4eeddcadbbb2a8 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Sun, 16 Jun 2024 21:36:38 +0200 Subject: [PATCH] [backend/logging] Display log level in text form in systemd logs --- .../Extensions/ConsoleLoggerExtensions.cs | 76 ++++++++++++++++++- .../Core/Extensions/ServiceExtensions.cs | 11 --- Iceshrimp.Backend/Startup.cs | 2 +- 3 files changed, 74 insertions(+), 15 deletions(-) diff --git a/Iceshrimp.Backend/Core/Extensions/ConsoleLoggerExtensions.cs b/Iceshrimp.Backend/Core/Extensions/ConsoleLoggerExtensions.cs index 68370a14..e002b87f 100644 --- a/Iceshrimp.Backend/Core/Extensions/ConsoleLoggerExtensions.cs +++ b/Iceshrimp.Backend/Core/Extensions/ConsoleLoggerExtensions.cs @@ -5,9 +5,21 @@ namespace Iceshrimp.Backend.Core.Extensions; public static class ConsoleLoggerExtensions { - public static ILoggingBuilder AddCustomConsoleFormatter(this ILoggingBuilder builder) => - builder.AddConsole(options => options.FormatterName = "custom") - .AddConsoleFormatter(); + public static ILoggingBuilder AddCustomConsoleFormatter(this ILoggingBuilder builder) + { + if (Environment.GetEnvironmentVariable("INVOCATION_ID") is null) + { + builder.AddConsole(options => options.FormatterName = "systemd-custom") + .AddConsoleFormatter(); + } + else + { + builder.AddConsole(options => options.FormatterName = "custom") + .AddConsoleFormatter(); + } + + return builder; + } } /* @@ -249,4 +261,62 @@ file sealed class CustomFormatter() : ConsoleFormatter("custom") } } +file sealed class CustomSystemdConsoleFormatter() : ConsoleFormatter("systemd-custom") +{ + public override void Write( + in LogEntry logEntry, + IExternalScopeProvider? scopeProvider, + TextWriter textWriter + ) + { + var message = logEntry.Formatter(logEntry.State, logEntry.Exception); + // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract + if (logEntry.Exception == null && message == null) return; + var logLevel = logEntry.LogLevel; + var category = logEntry.Category; + var id = logEntry.EventId.Id; + var exception = logEntry.Exception; + var syslogSeverityString = GetSyslogSeverityString(logLevel); + textWriter.Write(syslogSeverityString); + + textWriter.Write(category); + textWriter.Write('['); + textWriter.Write(id); + textWriter.Write(']'); + if (!string.IsNullOrEmpty(message)) + { + textWriter.Write(' '); + WriteReplacingNewLine(textWriter, message); + } + + if (exception != null) + { + textWriter.Write(' '); + WriteReplacingNewLine(textWriter, exception.ToString()); + } + + textWriter.Write(Environment.NewLine); + + static void WriteReplacingNewLine(TextWriter writer, string message) + { + var str = message.Replace(Environment.NewLine, " "); + writer.Write(str); + } + } + + private static string GetSyslogSeverityString(LogLevel logLevel) + { + return logLevel switch + { + LogLevel.Trace => "<7>trce: ", + LogLevel.Debug => "<7>dbug: ", + LogLevel.Information => "<6>info: ", + LogLevel.Warning => "<4>warn: ", + LogLevel.Error => "<3>fail: ", + LogLevel.Critical => "<2>crit: ", + _ => throw new ArgumentOutOfRangeException(nameof(logLevel)) + }; + } +} + #endregion \ No newline at end of file diff --git a/Iceshrimp.Backend/Core/Extensions/ServiceExtensions.cs b/Iceshrimp.Backend/Core/Extensions/ServiceExtensions.cs index 0c9278dc..cb284168 100644 --- a/Iceshrimp.Backend/Core/Extensions/ServiceExtensions.cs +++ b/Iceshrimp.Backend/Core/Extensions/ServiceExtensions.cs @@ -177,17 +177,6 @@ public static class ServiceExtensions services.TryAdd(new ServiceDescriptor(typeof(T), key, typeof(T), contextLifetime)); } - public static void AddLoggingWithOptions(this IServiceCollection services) - { - services.AddLogging(logging => - { - if (Environment.GetEnvironmentVariable("INVOCATION_ID") is not null) - logging.AddSystemdConsole(); - else - logging.AddCustomConsoleFormatter(); - }); - } - public static void AddSwaggerGenWithOptions(this IServiceCollection services) { services.AddEndpointsApiExplorer(); diff --git a/Iceshrimp.Backend/Startup.cs b/Iceshrimp.Backend/Startup.cs index 700a6da6..bc06aa1d 100644 --- a/Iceshrimp.Backend/Startup.cs +++ b/Iceshrimp.Backend/Startup.cs @@ -18,7 +18,7 @@ builder.Services.AddControllers() .AddApiBehaviorOptions(); builder.Services.AddSwaggerGenWithOptions(); -builder.Services.AddLoggingWithOptions(); +builder.Services.AddLogging(logging => logging.AddCustomConsoleFormatter()); builder.Services.AddDatabaseContext(builder.Configuration); builder.Services.AddSlidingWindowRateLimiter(); builder.Services.AddCorsPolicies();