1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2024-11-23 19:42:51 +01:00
Minecraft-Phantom-Panel/Utils/Phantom.Utils.Logging/DefaultLogLevel.cs

43 lines
1.2 KiB
C#

using System.Diagnostics.CodeAnalysis;
using Serilog.Events;
namespace Phantom.Utils.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;
}
}