diff options
| author | JP Dillingham <jp@dillingham.me> | 2023-11-04 17:10:51 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-04 17:10:51 -0500 |
| commit | 7d2143412a6ef26a6142d4d71d4c64f0119e4815 (patch) | |
| tree | b14fdbe3c86d36a6ad083d7f7946bd0906d4bf97 | |
| parent | 8438098eb54bb586733bdeff8d675fed9f3744e8 (diff) | |
| parent | 71bb7eb382491ac8a970fc73c84ca44d9c7bde7a (diff) | |
| download | slskd-7d2143412a6ef26a6142d4d71d4c64f0119e4815.tar.gz | |
Merge pull request #991 from slskd/logs
Add an option to disable logging to disk
| -rw-r--r-- | config/slskd.example.yml | 1 | ||||
| -rw-r--r-- | docs/config.md | 12 | ||||
| -rw-r--r-- | src/slskd/Core/Options.cs | 9 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 27 |
4 files changed, 40 insertions, 9 deletions
diff --git a/config/slskd.example.yml b/config/slskd.example.yml index 138a5cb5..93a30a98 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -124,6 +124,7 @@ # incomplete: 43200 # 30 days # logs: 259200 # 180 days # logger: +# disk: true # loki: ~ # metrics: # enabled: false diff --git a/docs/config.md b/docs/config.md index c6325cfd..bd4cc124 100644 --- a/docs/config.md +++ b/docs/config.md @@ -838,18 +838,22 @@ instance_name: default ## Loggers -By default, the application logs to disk (`/logs` in the application directory). Logs can optionally be forwarded to external services, and the targets can be expanded to any service supported by a [Serilog Sink](https://github.com/serilog/serilog/wiki/Provided-Sinks). Support for targets is added on an as-needed basis and within reason. +By default, the application logs to console and to disk (`/logs` in the application directory); disk logging can be disabled. + +Logs can optionally be forwarded to external services, and the targets can be expanded to any service supported by a [Serilog Sink](https://github.com/serilog/serilog/wiki/Provided-Sinks). Support for targets is added on an as-needed basis and within reason. The current list of available targets is: -| Command Line | Environment Variable | Description | -| ------------ | -------------------------- | ---------------------------------- | -| `--loki` | `SLSKD_LOKI` | The URL to a Grafana Loki instance | +| Command Line | Environment Variable | Description | +| ------------------ | -------------------------- | ---------------------------------- | +| `--loki` | `SLSKD_LOKI` | The URL to a Grafana Loki instance | +| `--no-disk-logger` | `SLSKD_NO_DISK_LOGGER` | Disable logging to disk | #### **YAML** ```yaml logger: loki: ~ + disk: true ``` ## Metrics diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 85cf133e..62334742 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1082,6 +1082,15 @@ namespace slskd [Description("optional; url to a Grafana Loki instance to which to log")] [RequiresRestart] public string Loki { get; init; } = null; + + /// <summary> + /// Gets a value indicating whether to write logs to disk. + /// </summary> + [Argument(default, "no-disk-logger")] + [EnvironmentVariable("NO_DISK_LOGGER")] + [Description("disable logging to disk")] + [RequiresRestart] + public bool Disk { get; init; } = true; } /// <summary> diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 7a832ddb..ca991e3c 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -198,6 +198,11 @@ namespace slskd public static string DefaultIncompleteDirectory { get; private set; } /// <summary> + /// Gets the path where application logs are saved. + /// </summary> + public static string LogDirectory { get; private set; } = null; + + /// <summary> /// Gets a buffer containing the last few log events. /// </summary> public static ConcurrentFixedSizeQueue<LogRecord> LogBuffer { get; } = new ConcurrentFixedSizeQueue<LogRecord>(size: 100); @@ -315,6 +320,7 @@ namespace slskd // derive the application directory value and defaults that are dependent upon it AppDirectory ??= DefaultAppDirectory; DataDirectory = Path.Combine(AppDirectory, "data"); + LogDirectory = Path.Combine(AppDirectory, "logs"); DefaultConfigurationFile = Path.Combine(AppDirectory, $"{AppName}.yml"); DefaultDownloadsDirectory = Path.Combine(AppDirectory, "downloads"); @@ -407,6 +413,15 @@ namespace slskd Log.Information("Using configuration file {ConfigurationFile}", ConfigurationFile); Log.Information("Storing application data in {DataDirectory}", DataDirectory); + if (OptionsAtStartup.Logger.Disk) + { + Log.Information("Saving application logs to {LogDirectory}", LogDirectory); + } + else + { + Log.Information("Logging to disk is disabled"); + } + RecreateConfigurationFileIfMissing(ConfigurationFile); if (!string.IsNullOrEmpty(OptionsAtStartup.Logger.Loki)) @@ -949,11 +964,13 @@ namespace slskd .WriteTo.Console( outputTemplate: (OptionsAtStartup.Debug ? "[{SourceContext}] " : string.Empty) + "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}") .WriteTo.Async(config => - config.File( - Path.Combine(AppDirectory, "logs", $"{AppName}-.log"), - outputTemplate: (OptionsAtStartup.Debug ? "[{SourceContext}] " : string.Empty) + "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}", - rollingInterval: RollingInterval.Day, - retainedFileTimeLimit: TimeSpan.FromDays(OptionsAtStartup.Retention.Logs))) + config.Conditional( + e => OptionsAtStartup.Logger.Disk, + config => config.File( + Path.Combine(LogDirectory, $"{AppName}-.log"), + outputTemplate: (OptionsAtStartup.Debug ? "[{SourceContext}] " : string.Empty) + "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}", + rollingInterval: RollingInterval.Day, + retainedFileTimeLimit: TimeSpan.FromDays(OptionsAtStartup.Retention.Logs)))) .WriteTo.Conditional( e => !string.IsNullOrEmpty(OptionsAtStartup.Logger.Loki), config => config.GrafanaLoki( |
