Add experimental Elasticsearch logging output

This commit is contained in:
Ske 2020-08-26 22:29:24 +02:00
parent 84a91b3f75
commit 2e6b1826fc
4 changed files with 41 additions and 5 deletions

View File

@ -21,6 +21,7 @@ using PluralKit.Core;
using Sentry; using Sentry;
using Serilog; using Serilog;
using Serilog.Context;
using Serilog.Events; using Serilog.Events;
namespace PluralKit.Bot namespace PluralKit.Bot
@ -101,6 +102,19 @@ namespace PluralKit.Bot
async Task HandleEventInner() 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(); 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 // 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

View File

@ -9,6 +9,7 @@ namespace PluralKit.Core
public string InfluxUrl { get; set; } public string InfluxUrl { get; set; }
public string InfluxDb { get; set; } public string InfluxDb { get; set; }
public string LogDir { get; set; } public string LogDir { get; set; }
public string ElasticUrl { get; set; }
public LogEventLevel ConsoleLogLevel { get; set; } = LogEventLevel.Debug; public LogEventLevel ConsoleLogLevel { get; set; } = LogEventLevel.Debug;
public LogEventLevel FileLogLevel { get; set; } = LogEventLevel.Information; public LogEventLevel FileLogLevel { get; set; } = LogEventLevel.Information;

View File

@ -12,7 +12,9 @@ using Microsoft.Extensions.DependencyInjection;
using NodaTime; using NodaTime;
using Serilog; using Serilog;
using Serilog.Events;
using Serilog.Formatting.Compact; using Serilog.Formatting.Compact;
using Serilog.Sinks.Elasticsearch;
using Serilog.Sinks.SystemConsole.Themes; using Serilog.Sinks.SystemConsole.Themes;
namespace PluralKit.Core 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}"; 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) .ConfigureForNodaTime(DateTimeZoneProviders.Tzdb)
.MinimumLevel.Is(config.ConsoleLogLevel) .MinimumLevel.Is(config.ConsoleLogLevel)
.WriteTo.Async(a => .WriteTo.Async(a =>
@ -111,7 +114,7 @@ namespace PluralKit.Core
restrictedToMinimumLevel: config.FileLogLevel, restrictedToMinimumLevel: config.FileLogLevel,
formatProvider: new UTCTimestampFormatProvider(), formatProvider: new UTCTimestampFormatProvider(),
buffered: true); buffered: true);
a.File( a.File(
new RenderedCompactJsonFormatter(), new RenderedCompactJsonFormatter(),
(config.LogDir ?? "logs") + $"/pluralkit.{_component}.json", (config.LogDir ?? "logs") + $"/pluralkit.{_component}.json",
@ -121,9 +124,26 @@ namespace PluralKit.Core
buffered: true); buffered: true);
}) })
// TODO: render as UTC in the console, too? or just in log files // TODO: render as UTC in the console, too? or just in log files
.WriteTo.Async(a => .WriteTo.Async(a =>
a.Console(theme: AnsiConsoleTheme.Code, outputTemplate: outputTemplate, formatProvider: new UTCTimestampFormatProvider())) a.Console(theme: AnsiConsoleTheme.Code, outputTemplate: outputTemplate,
.CreateLogger(); 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();
} }
} }

View File

@ -33,6 +33,7 @@
<PackageReference Include="Serilog.NodaTime" Version="3.0.0-beta01" /> <PackageReference Include="Serilog.NodaTime" Version="3.0.0-beta01" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.4.1-dev-00071" /> <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.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="Serilog.Sinks.File" Version="4.1.0" />
<PackageReference Include="System.Interactive.Async" Version="4.0.0" /> <PackageReference Include="System.Interactive.Async" Version="4.0.0" />
</ItemGroup> </ItemGroup>