diff --git a/PluralKit.API/Startup.cs b/PluralKit.API/Startup.cs index acd6c7bd..974c615e 100644 --- a/PluralKit.API/Startup.cs +++ b/PluralKit.API/Startup.cs @@ -32,6 +32,7 @@ namespace PluralKit.API .AddScoped() .AddTransient(_ => Configuration.GetSection("PluralKit").Get() ?? new CoreConfig()) + .AddSingleton(svc => InitUtils.InitLogger(svc.GetRequiredService(), "api")) .AddSingleton(svc => new DbConnectionFactory(svc.GetRequiredService().Database)); } diff --git a/PluralKit.Bot/Bot.cs b/PluralKit.Bot/Bot.cs index 36edb58a..4acd9505 100644 --- a/PluralKit.Bot/Bot.cs +++ b/PluralKit.Bot/Bot.cs @@ -116,18 +116,8 @@ namespace PluralKit.Bot }) .AddSingleton() - .AddSingleton(svc => new LoggerConfiguration() - .ConfigureForNodaTime(DateTimeZoneProviders.Tzdb) - .WriteTo.File( - new CompactJsonFormatter(), - (svc.GetRequiredService().LogDir ?? "logs") + "/pluralkit.bot.log", - rollingInterval: RollingInterval.Day, - flushToDiskInterval: TimeSpan.FromSeconds(10), - buffered: true) - .WriteTo.Console(theme: AnsiConsoleTheme.Code) - .CreateLogger()) - - .BuildServiceProvider(); + .AddSingleton(svc => InitUtils.InitLogger(svc.GetRequiredService(), "bot")) + .BuildServiceProvider(); } class Bot { diff --git a/PluralKit.Bot/PluralKit.Bot.csproj b/PluralKit.Bot/PluralKit.Bot.csproj index 059f28ea..75aed6c6 100644 --- a/PluralKit.Bot/PluralKit.Bot.csproj +++ b/PluralKit.Bot/PluralKit.Bot.csproj @@ -15,10 +15,6 @@ - - - - diff --git a/PluralKit.Core/PluralKit.Core.csproj b/PluralKit.Core/PluralKit.Core.csproj index 0d2c4a76..76055dcc 100644 --- a/PluralKit.Core/PluralKit.Core.csproj +++ b/PluralKit.Core/PluralKit.Core.csproj @@ -20,6 +20,10 @@ + + + + diff --git a/PluralKit.Core/Utils.cs b/PluralKit.Core/Utils.cs index b0c89c22..49bf0842 100644 --- a/PluralKit.Core/Utils.cs +++ b/PluralKit.Core/Utils.cs @@ -13,6 +13,9 @@ using NodaTime; using NodaTime.Serialization.JsonNet; using NodaTime.Text; using Npgsql; +using Serilog; +using Serilog.Formatting.Compact; +using Serilog.Sinks.SystemConsole.Themes; namespace PluralKit @@ -299,6 +302,20 @@ namespace PluralKit SqlMapper.AddTypeHandler(new PassthroughTypeHandler()); } + public static ILogger InitLogger(CoreConfig config, string component) + { + return new LoggerConfiguration() + .ConfigureForNodaTime(DateTimeZoneProviders.Tzdb) + .WriteTo.File( + new CompactJsonFormatter(), + (config.LogDir ?? "logs") + $"/pluralkit.{component}.log", + rollingInterval: RollingInterval.Day, + flushToDiskInterval: TimeSpan.FromSeconds(10), + buffered: true) + .WriteTo.Console(theme: AnsiConsoleTheme.Code) + .CreateLogger(); + } + public static JsonSerializerSettings BuildSerializerSettings() => new JsonSerializerSettings().BuildSerializerSettings(); public static JsonSerializerSettings BuildSerializerSettings(this JsonSerializerSettings settings)