Add experimental Elasticsearch logging output
This commit is contained in:
		| @@ -21,6 +21,7 @@ using PluralKit.Core; | ||||
| using Sentry; | ||||
|  | ||||
| using Serilog; | ||||
| using Serilog.Context; | ||||
| using Serilog.Events; | ||||
|  | ||||
| namespace PluralKit.Bot | ||||
| @@ -101,6 +102,19 @@ namespace PluralKit.Bot | ||||
|  | ||||
|             async Task HandleEventInner() | ||||
|             { | ||||
|                 // Mainly for testing ELK volume atm, no-op unless Elastic is configured | ||||
|                 if (evt is MessageCreateEventArgs mc) | ||||
|                     using (LogContext.PushProperty("Elastic", "yes?")) | ||||
|                         _logger.Information("Received event {@Event}", new | ||||
|                         { | ||||
|                             Id = Guid.NewGuid(), | ||||
|                             Type = mc.GetType().Name.Replace("EventArgs", ""), | ||||
|                             MessageId = mc.Message.Id, | ||||
|                             ChannelId = mc.Channel.Id, | ||||
|                             GuildId = mc.Guild?.Id ?? 0, | ||||
|                             UserId = mc.Author.Id, | ||||
|                         }); | ||||
|                      | ||||
|                 await using var serviceScope = _services.BeginLifetimeScope(); | ||||
|                  | ||||
|                 // Also, find a Sentry enricher for the event type (if one is present), and ask it to put some event data in the Sentry scope | ||||
|   | ||||
| @@ -9,6 +9,7 @@ namespace PluralKit.Core | ||||
|         public string InfluxUrl { get; set; } | ||||
|         public string InfluxDb { get; set; } | ||||
|         public string LogDir { get; set; } | ||||
|         public string ElasticUrl { get; set; } | ||||
|  | ||||
|         public LogEventLevel ConsoleLogLevel { get; set; } = LogEventLevel.Debug; | ||||
|         public LogEventLevel FileLogLevel { get; set; } = LogEventLevel.Information; | ||||
|   | ||||
| @@ -12,7 +12,9 @@ using Microsoft.Extensions.DependencyInjection; | ||||
| using NodaTime; | ||||
|  | ||||
| using Serilog; | ||||
| using Serilog.Events; | ||||
| using Serilog.Formatting.Compact; | ||||
| using Serilog.Sinks.Elasticsearch; | ||||
| using Serilog.Sinks.SystemConsole.Themes; | ||||
|  | ||||
| namespace PluralKit.Core | ||||
| @@ -95,7 +97,8 @@ namespace PluralKit.Core | ||||
|         { | ||||
|             var outputTemplate = "[{Timestamp:yyyy-MM-dd HH:mm:ss.ffffff}] {Level:u3} {Message:lj}{NewLine}{Exception}"; | ||||
|  | ||||
|             return new LoggerConfiguration() | ||||
|             var logger = new LoggerConfiguration() | ||||
|                 .Enrich.FromLogContext() | ||||
|                 .ConfigureForNodaTime(DateTimeZoneProviders.Tzdb) | ||||
|                 .MinimumLevel.Is(config.ConsoleLogLevel) | ||||
|                 .WriteTo.Async(a => | ||||
| @@ -111,7 +114,7 @@ namespace PluralKit.Core | ||||
|                         restrictedToMinimumLevel: config.FileLogLevel, | ||||
|                         formatProvider: new UTCTimestampFormatProvider(), | ||||
|                         buffered: true); | ||||
|                      | ||||
|  | ||||
|                     a.File( | ||||
|                         new RenderedCompactJsonFormatter(), | ||||
|                         (config.LogDir ?? "logs") + $"/pluralkit.{_component}.json", | ||||
| @@ -121,9 +124,26 @@ namespace PluralKit.Core | ||||
|                         buffered: true); | ||||
|                 }) | ||||
|                 // TODO: render as UTC in the console, too? or just in log files | ||||
|                 .WriteTo.Async(a =>  | ||||
|                     a.Console(theme: AnsiConsoleTheme.Code, outputTemplate: outputTemplate, formatProvider: new UTCTimestampFormatProvider())) | ||||
|                 .CreateLogger(); | ||||
|                 .WriteTo.Async(a => | ||||
|                     a.Console(theme: AnsiConsoleTheme.Code, outputTemplate: outputTemplate, | ||||
|                         formatProvider: new UTCTimestampFormatProvider())); | ||||
|  | ||||
|             if (config.ElasticUrl != null) | ||||
|             { | ||||
|                 var elasticConfig = new ElasticsearchSinkOptions(new Uri(config.ElasticUrl)) | ||||
|                 { | ||||
|                     AutoRegisterTemplate = true, | ||||
|                     AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7, | ||||
|                     MinimumLogEventLevel = LogEventLevel.Verbose, | ||||
|                     IndexFormat = "pluralkit-logs-{0:yyyy.MM.dd}" | ||||
|                 }; | ||||
|                 | ||||
|                 logger.WriteTo | ||||
|                     .Conditional(e => e.Properties.ContainsKey("Elastic"),  | ||||
|                         c => c.Elasticsearch(elasticConfig)); | ||||
|             } | ||||
|  | ||||
|             return logger.CreateLogger(); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -33,6 +33,7 @@ | ||||
|       <PackageReference Include="Serilog.NodaTime" Version="3.0.0-beta01" /> | ||||
|       <PackageReference Include="Serilog.Sinks.Async" Version="1.4.1-dev-00071" /> | ||||
|       <PackageReference Include="Serilog.Sinks.Console" Version="4.0.0-dev-00834" /> | ||||
|       <PackageReference Include="Serilog.Sinks.Elasticsearch" Version="8.2.0" /> | ||||
|       <PackageReference Include="Serilog.Sinks.File" Version="4.1.0" /> | ||||
|       <PackageReference Include="System.Interactive.Async" Version="4.0.0" /> | ||||
|     </ItemGroup> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user