aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-01-16 16:12:20 -0600
committerJP Dillingham <jpdillingham@gmail.com>2021-01-16 16:12:20 -0600
commit092626f188af4c062ed1b09363359850b774ef3b (patch)
treeb8242f68398b564bdbc14505282fd1a881c88093 /src
parent1eb59fdad8a16bca350564398ff0ae26025790c6 (diff)
downloadslskd-092626f188af4c062ed1b09363359850b774ef3b.tar.gz
configure logging
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Program.cs100
1 files changed, 84 insertions, 16 deletions
diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs
index 537c7f2e..6719709c 100644
--- a/src/slskd/Program.cs
+++ b/src/slskd/Program.cs
@@ -3,17 +3,24 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
+ using Serilog.Sinks.Grafana.Loki;
using System;
+ using System.Diagnostics;
using System.Linq;
using System.Reflection;
public class Program
{
+ public static bool Debug { get; private set; }
+ public static bool EnableLoki { get; private set; }
+ public static string LokiUrl { get; private set; }
+
public static void Main(string[] args)
{
+ var runId = Guid.NewGuid();
+
var assembly = Assembly.GetExecutingAssembly();
var assemblyVersion = assembly.GetName().Version;
var informationVersion = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
@@ -25,14 +32,73 @@
return;
}
+ if (args.Any(a => a == "--help" || a == "-h"))
+ {
+ PrintHelp();
+ return;
+ }
+
+ Debug = Debugger.IsAttached
+ || args.Any(a => a == "--debug" || a == "-d")
+ || Environment.GetEnvironmentVariable("SLSKD_DEBUG") != null;
+
+ LokiUrl = Environment.GetEnvironmentVariable("SLSKD_LOKI_URL");
+ EnableLoki = !string.IsNullOrWhiteSpace(LokiUrl) && Environment.GetEnvironmentVariable("SLSKD_ENABLE_LOKI") != null;
+
+ if (Debug)
+ {
+ Log.Logger = new LoggerConfiguration()
+ .MinimumLevel.Debug()
+ .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
+ .Enrich.WithProperty("RunId", runId)
+ .Enrich.FromLogContext()
+ .WriteTo.Console(
+ outputTemplate: "[{SourceContext}] [{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
+ .WriteTo.Async(config =>
+ config.File(
+ "logs/slskd-.log",
+ outputTemplate: "[{SourceContext}] [{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}",
+ rollingInterval: RollingInterval.Day))
+ .WriteTo.Conditional(
+ e => EnableLoki,
+ config => config.GrafanaLoki(
+ LokiUrl,
+ outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"))
+ .CreateLogger();
+ }
+ else
+ {
+ Log.Logger = new LoggerConfiguration()
+ .MinimumLevel.Information()
+ .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
+ .MinimumLevel.Override("slskd.Security.PassthroughAuthenticationHandler", LogEventLevel.Information)
+ .Enrich.WithProperty("RunId", runId)
+ .Enrich.FromLogContext()
+ .WriteTo.Console(
+ outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
+ .WriteTo.Async(config =>
+ config.File(
+ "logs/slskd-.log",
+ outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}",
+ rollingInterval: RollingInterval.Day))
+ .WriteTo.Conditional(
+ e => EnableLoki,
+ config => config.GrafanaLoki(
+ LokiUrl,
+ outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"))
+ .CreateLogger();
+ }
+
PrintBanner(version);
- Log.Logger = new LoggerConfiguration()
- .MinimumLevel.Debug()
- .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
- .Enrich.FromLogContext()
- .WriteTo.Console()
- .CreateLogger();
+ var logger = Log.ForContext<Program>();
+
+ logger.Information("Run ID: {RunId}", runId);
+
+ if (EnableLoki)
+ {
+ logger.Information("Logging to Loki instance at {LokiUrl}", LokiUrl);
+ }
try
{
@@ -40,7 +106,7 @@
}
catch (Exception ex)
{
- Log.Fatal(ex, "Application terminated unexpectedly");
+ logger.Fatal(ex, "Application terminated unexpectedly");
}
finally
{
@@ -50,19 +116,12 @@
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
- .UseSerilog()
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddEnvironmentVariables(prefix: "SLSK_");
config.AddJsonFile("config.json", optional: true, reloadOnChange: false);
})
- .ConfigureLogging((context, logging) =>
- {
- if (!System.Diagnostics.Debugger.IsAttached)
- {
- logging.ClearProviders();
- }
- })
+ .UseSerilog()
.UseStartup<Startup>();
public static void PrintBanner(string version)
@@ -85,5 +144,14 @@
Console.WriteLine(banner);
}
+
+ public static void PrintHelp()
+ {
+ Console.WriteLine(@"
+usage: slskd [options]
+ --version|-v displays the current version
+ --debug|-d runs the daemon in debug mode
+ --help|-h prints this help");
+ }
}
}