2020-04-28 22:25:31 +00:00
|
|
|
using System.Globalization;
|
2020-01-26 00:27:45 +00:00
|
|
|
|
|
|
|
using Autofac;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
2020-01-26 00:27:45 +00:00
|
|
|
using NodaTime;
|
|
|
|
|
|
|
|
using Serilog;
|
2020-08-26 20:29:24 +00:00
|
|
|
using Serilog.Events;
|
2020-01-26 00:27:45 +00:00
|
|
|
using Serilog.Formatting.Compact;
|
2020-08-26 20:29:24 +00:00
|
|
|
using Serilog.Sinks.Elasticsearch;
|
2020-01-26 00:27:45 +00:00
|
|
|
using Serilog.Sinks.SystemConsole.Themes;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
using ILogger = Serilog.ILogger;
|
|
|
|
|
|
|
|
namespace PluralKit.Core;
|
|
|
|
|
|
|
|
public class LoggingModule: Module
|
2020-01-26 00:27:45 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly string _component;
|
|
|
|
private readonly Action<LoggerConfiguration> _fn;
|
|
|
|
|
|
|
|
public LoggingModule(string component, Action<LoggerConfiguration> fn = null, LoggerConfiguration cfg = null)
|
2020-01-26 00:27:45 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
_component = component;
|
2022-03-30 08:36:22 +00:00
|
|
|
// todo: this is messy and not really used anywhere...?
|
2021-11-27 02:10:56 +00:00
|
|
|
_fn = fn ?? (_ => { });
|
|
|
|
_cfg = cfg ?? new LoggerConfiguration();
|
|
|
|
}
|
2020-01-26 00:27:45 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private LoggerConfiguration _cfg { get; }
|
2020-01-26 00:27:45 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
protected override void Load(ContainerBuilder builder)
|
|
|
|
{
|
|
|
|
builder
|
|
|
|
.Register(c => InitLogger(c.Resolve<CoreConfig>()))
|
|
|
|
.AsSelf()
|
|
|
|
.SingleInstance()
|
|
|
|
// AutoActivate ensures logging is enabled as early as possible in the API startup flow
|
|
|
|
// since we set the Log.Logger global >.>
|
|
|
|
.AutoActivate();
|
|
|
|
|
|
|
|
builder.Register(c => new LoggerFactory().AddSerilog(c.Resolve<ILogger>()))
|
|
|
|
.As<ILoggerFactory>()
|
|
|
|
.SingleInstance();
|
|
|
|
}
|
2020-01-26 00:27:45 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private ILogger InitLogger(CoreConfig config)
|
|
|
|
{
|
|
|
|
var consoleTemplate = "[{Timestamp:HH:mm:ss.fff}] {Level:u3} {Message:lj}{NewLine}{Exception}";
|
|
|
|
var outputTemplate = "[{Timestamp:yyyy-MM-dd HH:mm:ss.ffffff}] {Level:u3} {Message:lj}{NewLine}{Exception}";
|
|
|
|
|
|
|
|
var logCfg = _cfg
|
|
|
|
.Enrich.FromLogContext()
|
2022-01-24 13:13:59 +00:00
|
|
|
.Enrich.WithProperty("GitCommitHash", BuildInfoService.FullVersion)
|
2021-11-27 02:10:56 +00:00
|
|
|
.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb)
|
|
|
|
.Enrich.WithProperty("Component", _component)
|
|
|
|
.MinimumLevel.Is(config.ConsoleLogLevel)
|
|
|
|
|
|
|
|
// Don't want App.Metrics/D#+ spam
|
|
|
|
.MinimumLevel.Override("App.Metrics", LogEventLevel.Information)
|
|
|
|
|
|
|
|
// nor ASP.NET spam
|
|
|
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
|
|
|
|
|
|
|
// Actual formatting for these is handled in ScalarFormatting
|
|
|
|
.Destructure.AsScalar<SystemId>()
|
|
|
|
.Destructure.AsScalar<MemberId>()
|
|
|
|
.Destructure.AsScalar<GroupId>()
|
|
|
|
.Destructure.AsScalar<SwitchId>()
|
|
|
|
.Destructure.ByTransforming<ProxyTag>(t => new { t.Prefix, t.Suffix })
|
|
|
|
.Destructure.With<PatchObjectDestructuring>()
|
|
|
|
.WriteTo.Async(a =>
|
|
|
|
{
|
|
|
|
// Both the same output, except one is raw compact JSON and one is plain text.
|
|
|
|
// Output simultaneously. May remove the JSON formatter later, keeping it just in cast.
|
|
|
|
// Flush interval is 50ms (down from 10s) to make "tail -f" easier. May be too low?
|
|
|
|
a.File(
|
|
|
|
(config.LogDir ?? "logs") + $"/pluralkit.{_component}.log",
|
|
|
|
outputTemplate: outputTemplate,
|
|
|
|
retainedFileCountLimit: 10,
|
|
|
|
rollingInterval: RollingInterval.Day,
|
|
|
|
fileSizeLimitBytes: null,
|
|
|
|
flushToDiskInterval: TimeSpan.FromMilliseconds(50),
|
|
|
|
restrictedToMinimumLevel: config.FileLogLevel,
|
|
|
|
formatProvider: new UTCTimestampFormatProvider(),
|
|
|
|
buffered: true);
|
|
|
|
|
|
|
|
a.File(
|
|
|
|
new RenderedCompactJsonFormatter(new ScalarFormatting.JsonValue()),
|
|
|
|
(config.LogDir ?? "logs") + $"/pluralkit.{_component}.json",
|
|
|
|
rollingInterval: RollingInterval.Day,
|
|
|
|
flushToDiskInterval: TimeSpan.FromMilliseconds(50),
|
|
|
|
restrictedToMinimumLevel: config.FileLogLevel,
|
|
|
|
buffered: true);
|
|
|
|
})
|
|
|
|
.WriteTo.Async(a =>
|
|
|
|
a.Console(
|
|
|
|
theme: AnsiConsoleTheme.Code,
|
|
|
|
outputTemplate: consoleTemplate,
|
|
|
|
restrictedToMinimumLevel: config.ConsoleLogLevel));
|
|
|
|
|
|
|
|
if (config.ElasticUrl != null)
|
2020-01-26 00:27:45 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
var elasticConfig = new ElasticsearchSinkOptions(new Uri(config.ElasticUrl))
|
2020-08-26 20:29:24 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
AutoRegisterTemplate = true,
|
|
|
|
AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7,
|
|
|
|
MinimumLogEventLevel = config.ElasticLogLevel,
|
|
|
|
IndexFormat = "pluralkit-logs-{0:yyyy.MM.dd}",
|
|
|
|
CustomFormatter = new ScalarFormatting.Elasticsearch()
|
|
|
|
};
|
|
|
|
|
|
|
|
logCfg.WriteTo.Elasticsearch(elasticConfig);
|
2020-01-26 00:27:45 +00:00
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
_fn.Invoke(logCfg);
|
|
|
|
return Log.Logger = logCfg.CreateLogger();
|
2020-04-28 22:25:31 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
2020-04-28 22:25:31 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// Serilog why is this necessary for such a simple thing >.>
|
|
|
|
public class UTCTimestampFormatProvider: IFormatProvider
|
|
|
|
{
|
|
|
|
public object GetFormat(Type formatType) => new UTCTimestampFormatter();
|
|
|
|
}
|
|
|
|
|
|
|
|
public class UTCTimestampFormatter: ICustomFormatter
|
|
|
|
{
|
|
|
|
public string Format(string format, object arg, IFormatProvider formatProvider)
|
2020-04-28 22:25:31 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
// Convert offset to UTC and then print
|
|
|
|
// FormatProvider defaults to locale-specific stuff so we force-default to invariant culture
|
|
|
|
// If we pass the given formatProvider it'll conveniently ignore it, for some reason >.>
|
|
|
|
if (arg is DateTimeOffset dto)
|
|
|
|
return dto.ToUniversalTime().ToString(format, CultureInfo.InvariantCulture);
|
|
|
|
if (arg is IFormattable f)
|
|
|
|
return f.ToString(format, CultureInfo.InvariantCulture);
|
|
|
|
return arg.ToString();
|
2020-04-28 22:25:31 +00:00
|
|
|
}
|
2020-01-26 00:27:45 +00:00
|
|
|
}
|