diff options
| author | Joshua M. Boniface <joshua@boniface.me> | 2025-08-03 17:27:17 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-03 17:27:17 -0400 |
| commit | 4b6fb6c4bb2478badad068ce18aabe0c2955db48 (patch) | |
| tree | 15f986ee62327cceb8f5c8f009bcf08d10cfaa66 /Jellyfin.Server/ServerSetupApp/StartupLogger.cs | |
| parent | e7bc86ebb8496615e0b3f73eb4f13ab4c0913dc8 (diff) | |
| parent | db7465e83d9cc07134a0bffad7ed17b1c7b873da (diff) | |
Merge branch 'master' into master
Diffstat (limited to 'Jellyfin.Server/ServerSetupApp/StartupLogger.cs')
| -rw-r--r-- | Jellyfin.Server/ServerSetupApp/StartupLogger.cs | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/Jellyfin.Server/ServerSetupApp/StartupLogger.cs b/Jellyfin.Server/ServerSetupApp/StartupLogger.cs new file mode 100644 index 000000000..0121854ce --- /dev/null +++ b/Jellyfin.Server/ServerSetupApp/StartupLogger.cs @@ -0,0 +1,124 @@ +using System; +using System.Globalization; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; + +namespace Jellyfin.Server.ServerSetupApp; + +/// <inheritdoc/> +public class StartupLogger : IStartupLogger +{ + private readonly StartupLogTopic? _topic; + + /// <summary> + /// Initializes a new instance of the <see cref="StartupLogger"/> class. + /// </summary> + /// <param name="logger">The underlying base logger.</param> + public StartupLogger(ILogger logger) + { + BaseLogger = logger; + } + + /// <summary> + /// Initializes a new instance of the <see cref="StartupLogger"/> class. + /// </summary> + /// <param name="logger">The underlying base logger.</param> + /// <param name="topic">The group for this logger.</param> + internal StartupLogger(ILogger logger, StartupLogTopic? topic) : this(logger) + { + _topic = topic; + } + + internal static IStartupLogger Logger { get; set; } = new StartupLogger(NullLogger.Instance); + + /// <inheritdoc/> + public StartupLogTopic? Topic => _topic; + + /// <summary> + /// Gets or Sets the underlying base logger. + /// </summary> + protected ILogger BaseLogger { get; set; } + + /// <inheritdoc/> + public IStartupLogger BeginGroup(FormattableString logEntry) + { + return new StartupLogger(BaseLogger, AddToTopic(logEntry)); + } + + /// <inheritdoc/> + public IStartupLogger With(ILogger logger) + { + return new StartupLogger(logger, Topic); + } + + /// <inheritdoc/> + public IStartupLogger<TCategory> With<TCategory>(ILogger logger) + { + return new StartupLogger<TCategory>(logger, Topic); + } + + /// <inheritdoc/> + public IStartupLogger<TCategory> BeginGroup<TCategory>(FormattableString logEntry) + { + return new StartupLogger<TCategory>(BaseLogger, AddToTopic(logEntry)); + } + + private StartupLogTopic AddToTopic(FormattableString logEntry) + { + var startupEntry = new StartupLogTopic() + { + Content = logEntry.ToString(CultureInfo.InvariantCulture), + DateOfCreation = DateTimeOffset.Now + }; + + if (Topic is null) + { + SetupServer.LogQueue?.Enqueue(startupEntry); + } + else + { + Topic.Children.Add(startupEntry); + } + + return startupEntry; + } + + /// <inheritdoc/> + public IDisposable? BeginScope<TState>(TState state) + where TState : notnull + { + return null; + } + + /// <inheritdoc/> + public bool IsEnabled(LogLevel logLevel) + { + return true; + } + + /// <inheritdoc/> + public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) + { + if (BaseLogger.IsEnabled(logLevel)) + { + // if enabled allow the base logger also to receive the message + BaseLogger.Log(logLevel, eventId, state, exception, formatter); + } + + var startupEntry = new StartupLogTopic() + { + LogLevel = logLevel, + Content = formatter(state, exception), + DateOfCreation = DateTimeOffset.Now + }; + + if (Topic is null) + { + SetupServer.LogQueue?.Enqueue(startupEntry); + } + else + { + Topic.Children.Add(startupEntry); + } + } +} |
