From b85c79c57b130e28e7e0f490c590ab099284b514 Mon Sep 17 00:00:00 2001 From: Ske Date: Fri, 19 Jul 2019 02:29:08 +0200 Subject: [PATCH] Expand logging capability --- PluralKit.Bot/Bot.cs | 43 +++++++++++++++++++-- PluralKit.Bot/Services/LogChannelService.cs | 2 +- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/PluralKit.Bot/Bot.cs b/PluralKit.Bot/Bot.cs index 4acd9505..85385b18 100644 --- a/PluralKit.Bot/Bot.cs +++ b/PluralKit.Bot/Bot.cs @@ -15,6 +15,7 @@ using NodaTime; using Sentry; using Serilog; using Serilog.Core; +using Serilog.Events; using Serilog.Formatting.Compact; using Serilog.Sinks.SystemConsole.Themes; @@ -43,22 +44,24 @@ namespace PluralKit.Bot using (var services = BuildServiceProvider()) { + var logger = services.GetRequiredService().ForContext(); var coreConfig = services.GetRequiredService(); var botConfig = services.GetRequiredService(); using (SentrySdk.Init(coreConfig.SentryUrl)) { - Console.WriteLine("- Connecting to database..."); + logger.Information("Connecting to database"); using (var conn = await services.GetRequiredService().Obtain()) await Schema.CreateTables(conn); - Console.WriteLine("- Connecting to Discord..."); + logger.Information("Connecting to Discord"); var client = services.GetRequiredService() as DiscordShardedClient; await client.LoginAsync(TokenType.Bot, botConfig.Token); - Console.WriteLine("- Initializing bot..."); + logger.Information("Initializing bot"); await services.GetRequiredService().Init(); + await client.StartAsync(); @@ -67,7 +70,7 @@ namespace PluralKit.Bot await Task.Delay(-1, token.Token); } catch (TaskCanceledException) { } // We'll just exit normally - Console.WriteLine("- Shutting down..."); + logger.Information("Shutting down"); } } } @@ -155,6 +158,38 @@ namespace PluralKit.Bot _client.MessageReceived += async (msg) => MessageReceived(msg).CatchException(HandleRuntimeError); _client.ReactionAdded += async (message, channel, reaction) => _proxy.HandleReactionAddedAsync(message, channel, reaction).CatchException(HandleRuntimeError); _client.MessageDeleted += async (message, channel) => _proxy.HandleMessageDeletedAsync(message, channel).CatchException(HandleRuntimeError); + + _client.Log += FrameworkLog; + } + + private Task FrameworkLog(LogMessage msg) + { + // Bridge D.NET logging to Serilog + LogEventLevel level = LogEventLevel.Verbose; + switch (msg.Severity) + { + case LogSeverity.Critical: + level = LogEventLevel.Fatal; + break; + case LogSeverity.Debug: + level = LogEventLevel.Debug; + break; + case LogSeverity.Error: + level = LogEventLevel.Error; + break; + case LogSeverity.Info: + level = LogEventLevel.Information; + break; + case LogSeverity.Verbose: + level = LogEventLevel.Verbose; + break; + case LogSeverity.Warning: + level = LogEventLevel.Warning; + break; + } + + _logger.Write(level, msg.Exception, "Discord.Net {Source}: {Message}", msg.Source, msg.Message); + return Task.CompletedTask; } // Method called every 60 seconds diff --git a/PluralKit.Bot/Services/LogChannelService.cs b/PluralKit.Bot/Services/LogChannelService.cs index 939dad2d..a16f36bf 100644 --- a/PluralKit.Bot/Services/LogChannelService.cs +++ b/PluralKit.Bot/Services/LogChannelService.cs @@ -57,7 +57,7 @@ namespace PluralKit.Bot { def); } - _logger.Information("Set guild {} log channel to {Channel}", guild.Id, newLogChannel?.Id); + _logger.Information("Set guild {Guild} log channel to {Channel}", guild.Id, newLogChannel?.Id); } } } \ No newline at end of file