diff --git a/PluralKit.Bot/Bot.cs b/PluralKit.Bot/Bot.cs index d0d39793..315d5300 100644 --- a/PluralKit.Bot/Bot.cs +++ b/PluralKit.Bot/Bot.cs @@ -11,13 +11,9 @@ using Discord.Commands; using Discord.WebSocket; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using NodaTime; using Sentry; using Serilog; -using Serilog.Core; using Serilog.Events; -using Serilog.Formatting.Compact; -using Serilog.Sinks.SystemConsole.Themes; namespace PluralKit.Bot { @@ -130,7 +126,10 @@ namespace PluralKit.Bot .AddScoped(_ => new Sentry.Scope(null)) .AddTransient() - .AddSingleton(svc => InitUtils.InitLogger(svc.GetRequiredService(), "bot")) + .AddScoped() + .AddSingleton(svc => new LoggerProvider(svc.GetRequiredService(), "bot")) + .AddScoped(svc => svc.GetRequiredService().RootLogger.ForContext("EventId", svc.GetRequiredService().EventId)) + .BuildServiceProvider(); } class Bot @@ -333,6 +332,8 @@ namespace PluralKit.Bot arg.HasStringPrefix("pk!", ref argPos, StringComparison.OrdinalIgnoreCase) || arg.HasMentionPrefix(_client.CurrentUser, ref argPos)) { + _logger.Debug("Parsing command {Command} from message {Channel}-{Message}", msg.Content, msg.Channel.Id, msg.Id); + // Essentially move the argPos pointer by however much whitespace is at the start of the post-argPos string var trimStartLengthDiff = arg.Content.Substring(argPos).Length - arg.Content.Substring(argPos).TrimStart().Length; diff --git a/PluralKit.Core/Utils.cs b/PluralKit.Core/Utils.cs index a89ef041..249fb786 100644 --- a/PluralKit.Core/Utils.cs +++ b/PluralKit.Core/Utils.cs @@ -17,7 +17,11 @@ using NodaTime.Text; using Npgsql; using PluralKit.Core; using Serilog; +using Serilog.Core; +using Serilog.Events; using Serilog.Formatting.Compact; +using Serilog.Formatting.Display; +using Serilog.Formatting.Json; using Serilog.Sinks.SystemConsole.Themes; @@ -300,17 +304,19 @@ namespace PluralKit SqlMapper.AddTypeHandler(new PassthroughTypeHandler()); } - public static ILogger InitLogger(CoreConfig config, string component) + public static ILogger InitLogger(CoreConfig config, string component) { return new LoggerConfiguration() .ConfigureForNodaTime(DateTimeZoneProviders.Tzdb) + .MinimumLevel.Debug() .WriteTo.File( - new CompactJsonFormatter(), + new RenderedCompactJsonFormatter(), (config.LogDir ?? "logs") + $"/pluralkit.{component}.log", rollingInterval: RollingInterval.Day, flushToDiskInterval: TimeSpan.FromSeconds(10), + restrictedToMinimumLevel: LogEventLevel.Information, buffered: true) - .WriteTo.Console(theme: AnsiConsoleTheme.Code) + .WriteTo.Console(theme: AnsiConsoleTheme.Code, outputTemplate:"[{Timestamp:HH:mm:ss}] [{EventId}] {Level:u3} {Message:lj}{NewLine}{Exception}") .CreateLogger(); } @@ -322,7 +328,19 @@ namespace PluralKit return settings; } } - + + public class LoggerProvider + { + private CoreConfig _config; + public ILogger RootLogger { get; } + + public LoggerProvider(CoreConfig config, string component) + { + _config = config; + RootLogger = InitUtils.InitLogger(_config, component); + } + } + public class UlongEncodeAsLongHandler : SqlMapper.TypeHandler { public override ulong Parse(object value) @@ -457,4 +475,14 @@ namespace PluralKit public ConnectionState State => _impl.State; } + + public class EventIdProvider + { + public Guid EventId { get; } + + public EventIdProvider() + { + EventId = Guid.NewGuid(); + } + } }