mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2025-04-10 20:15:44 +02:00
Add environment variable to set minimum log level
This commit is contained in:
parent
a192a9aa54
commit
d50119d666
42
Common/Phantom.Common.Logging/DefaultLogLevel.cs
Normal file
42
Common/Phantom.Common.Logging/DefaultLogLevel.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Serilog.Events;
|
||||
|
||||
namespace Phantom.Common.Logging;
|
||||
|
||||
static class DefaultLogLevel {
|
||||
private const string ENVIRONMENT_VARIABLE = "LOG_LEVEL";
|
||||
|
||||
public static LogEventLevel Value { get; } = GetDefaultLevel();
|
||||
|
||||
public static LogEventLevel Coerce(LogEventLevel level) {
|
||||
return level < Value ? Value : level;
|
||||
}
|
||||
|
||||
private static LogEventLevel GetDefaultLevel() {
|
||||
var level = Environment.GetEnvironmentVariable(ENVIRONMENT_VARIABLE);
|
||||
return level switch {
|
||||
"VERBOSE" => LogEventLevel.Verbose,
|
||||
"DEBUG" => LogEventLevel.Debug,
|
||||
"INFORMATION" => LogEventLevel.Information,
|
||||
"WARNING" => LogEventLevel.Warning,
|
||||
"ERROR" => LogEventLevel.Error,
|
||||
null => GetDefaultLevelFallback(),
|
||||
_ => LogEnvironmentVariableErrorAndExit(level)
|
||||
};
|
||||
}
|
||||
|
||||
private static LogEventLevel GetDefaultLevelFallback() {
|
||||
#if DEBUG
|
||||
return LogEventLevel.Verbose;
|
||||
#else
|
||||
return LogEventLevel.Information;
|
||||
#endif
|
||||
}
|
||||
|
||||
[DoesNotReturn]
|
||||
private static LogEventLevel LogEnvironmentVariableErrorAndExit(string logLevel) {
|
||||
Console.Error.WriteLine("Invalid value of environment variable {0}: {1}", ENVIRONMENT_VARIABLE, logLevel);
|
||||
Environment.Exit(1);
|
||||
return LogEventLevel.Fatal;
|
||||
}
|
||||
}
|
@ -7,28 +7,21 @@ using Serilog.Sinks.SystemConsole.Themes;
|
||||
namespace Phantom.Common.Logging;
|
||||
|
||||
public static class PhantomLogger {
|
||||
public static Logger Root { get; } = CreateBaseLogger("[{Timestamp:HH:mm:ss} {Level:u}] {Message:lj}{NewLine}{Exception}");
|
||||
private static Logger Base { get; } = CreateBaseLogger("[{Timestamp:HH:mm:ss} {Level:u}] [{Category}] {Message:lj}{NewLine}{Exception}");
|
||||
|
||||
private static LogEventLevel GetDefaultLevel() {
|
||||
#if DEBUG
|
||||
return LogEventLevel.Verbose;
|
||||
#else
|
||||
return LogEventLevel.Information;
|
||||
#endif
|
||||
public static Logger Root { get; } = CreateLogger("[{Timestamp:HH:mm:ss} {Level:u}] {Message:lj}{NewLine}{Exception}");
|
||||
private static Logger Base { get; } = CreateLogger("[{Timestamp:HH:mm:ss} {Level:u}] [{Category}] {Message:lj}{NewLine}{Exception}");
|
||||
|
||||
private static Logger CreateLogger(string template) {
|
||||
return new LoggerConfiguration()
|
||||
.MinimumLevel.Is(DefaultLogLevel.Value)
|
||||
.MinimumLevel.Override("Microsoft", DefaultLogLevel.Coerce(LogEventLevel.Information))
|
||||
.MinimumLevel.Override("Microsoft.AspNetCore", DefaultLogLevel.Coerce(LogEventLevel.Warning))
|
||||
.MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", DefaultLogLevel.Coerce(LogEventLevel.Warning))
|
||||
.Filter.ByExcluding(static e => e.Exception is OperationCanceledException)
|
||||
.Enrich.FromLogContext()
|
||||
.WriteTo.Console(outputTemplate: template, formatProvider: CultureInfo.InvariantCulture, theme: AnsiConsoleTheme.Literate)
|
||||
.CreateLogger();
|
||||
}
|
||||
|
||||
private static Logger CreateBaseLogger(string template) =>
|
||||
new LoggerConfiguration()
|
||||
.MinimumLevel.Is(GetDefaultLevel())
|
||||
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
||||
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
|
||||
.MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", LogEventLevel.Warning)
|
||||
.Filter.ByExcluding(static e => e.Exception is OperationCanceledException)
|
||||
.Enrich.FromLogContext()
|
||||
.WriteTo.Console(outputTemplate: template, formatProvider: CultureInfo.InvariantCulture, theme: AnsiConsoleTheme.Literate)
|
||||
.CreateLogger();
|
||||
|
||||
public static ILogger Create(string name) {
|
||||
return Base.ForContext("Category", name);
|
||||
}
|
||||
|
12
README.md
12
README.md
@ -97,6 +97,18 @@ Use volumes to persist either the whole `/data` folder, or just `/data/data` if
|
||||
- `ALLOWED_SERVER_PORTS` is a comma-separated list of ports and port ranges that can be used as Minecraft Server ports. Example: `25565,25900,26000-27000`
|
||||
- `ALLOWED_RCON_PORTS` is a comma-separated list of ports and port ranges that can be used as Minecraft RCON ports. Example: `25575,25901,36000-37000`
|
||||
|
||||
## Logging
|
||||
|
||||
Both the Server and Agent support a `LOG_LEVEL` environment variable to set the minimum log level. Possible values:
|
||||
|
||||
* `VERBOSE`
|
||||
* `DEBUG`
|
||||
* `INFORMATION`
|
||||
* `WARNING`
|
||||
* `ERROR`
|
||||
|
||||
If the environment variable is omitted, the log level is set to `VERBOSE` for Debug builds and `INFORMATION` for Release builds.
|
||||
|
||||
# Development
|
||||
|
||||
The repository includes a [Rider](https://www.jetbrains.com/rider/) projects with several run configurations. The `.workdir` folder in the root of the repository is used for storage. Here's how to get started:
|
||||
|
Loading…
Reference in New Issue
Block a user