From 91a7b2554f11b2a63aeb59c9ecf27bc786a2d089 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Wed, 14 Aug 2024 04:30:22 +0200 Subject: [PATCH] [backend/logging] Add LOG_TIMESTAMPS environment variable --- .../Core/Extensions/ConsoleLoggerExtensions.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Iceshrimp.Backend/Core/Extensions/ConsoleLoggerExtensions.cs b/Iceshrimp.Backend/Core/Extensions/ConsoleLoggerExtensions.cs index cd632de8..5b4daf65 100644 --- a/Iceshrimp.Backend/Core/Extensions/ConsoleLoggerExtensions.cs +++ b/Iceshrimp.Backend/Core/Extensions/ConsoleLoggerExtensions.cs @@ -126,13 +126,17 @@ file static class ConsoleUtils file sealed class CustomFormatter() : ConsoleFormatter("custom"), ISupportExternalScope { + private static readonly bool IsSystemd = Environment.GetEnvironmentVariable("INVOCATION_ID") is not null; + private static readonly bool LogTimestamps = Environment.GetEnvironmentVariable("LOG_TIMESTAMPS") is "1" or "true"; + private const string LoglevelPadding = ": "; + // @formatter:off private static readonly string MessagePadding = - new(' ', GetLogLevelString(LogLevel.Information).Length + LoglevelPadding.Length); + new(' ', LogTimestamps ? DateTime.Now.ToDisplayStringTz().Length + 2 : 0 + GetLogLevelString(LogLevel.Information).Length + LoglevelPadding.Length); + // @formatter:on private static readonly string NewLineWithMessagePadding = Environment.NewLine + MessagePadding; - private static readonly bool IsSystemd = Environment.GetEnvironmentVariable("INVOCATION_ID") is not null; private IExternalScopeProvider? _scopeProvider; @@ -172,6 +176,12 @@ file sealed class CustomFormatter() : ConsoleFormatter("custom"), ISupportExtern var prefix = IsSystemd ? GetSyslogSeverityIndicatorString(logEntry.LogLevel) : null; if (prefix != null) textWriter.Write(prefix); + if (LogTimestamps) + { + textWriter.WriteColoredMessage(DateTime.Now.ToDisplayStringTz(), null, ConsoleColor.Green); + textWriter.WriteColoredMessage("| ", null, ConsoleColor.Gray); + } + textWriter.WriteColoredMessage(logLevelString, logLevelColors.Background, logLevelColors.Foreground); CreateDefaultLogMessage(textWriter, logEntry, message, scope, prefix);