New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can you provide custom log level string to SimpleConsoleFormatter #44773
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @maryamariyan Issue DetailsBackground and MotivationI like to use the built-in console logging in production because it is easy and it looks good. The thing that frustrates me when using it is its log level string behaviour. Namely we take the source code as example: runtime/src/libraries/Microsoft.Extensions.Logging.Console/src/SimpleConsoleFormatter.cs Line 134 in 3c801a8
return logLevel switch
{
LogLevel.Trace => "trce",
LogLevel.Debug => "dbug",
LogLevel.Information => "info",
LogLevel.Warning => "warn",
LogLevel.Error => "fail",
LogLevel.Critical => "crit",
_ => throw new ArgumentOutOfRangeException(nameof(logLevel))
}; This is hardcoded and, somehow, not as common as things like "DEBUG", "ERROR", etc. For example, when I was using LogDNA, which should automatically parse log and log levels, cannot infer "fail" because it thinks this pattern is not common. It would be very helpful to make this customisable. Proposed APII don't know, probably adding a configuration to namespace Microsoft.Extensions.Logging.Console
{
public class SimpleConsoleFormatterOptions : ConsoleFormatterOptions
{
public SimpleConsoleFormatterOptions() { }
public LoggerColorBehavior ColorBehavior { get; set; }
public bool SingleLine { get; set; }
+ public Func<LogLevel, string> LogLevelStringFormatter { get; set; }
}
} If the user does not set this, we can use the default strings as they are now. Usage ExamplesLike this: // builder is ILoggingBuilder
builder.AddSimpleConsole(options =>
{
options.LogLevelStringFormatter = logLevel => logLevel switch
{
LogLevel.Trace => "trce",
LogLevel.Debug => "dbug",
LogLevel.Information => "info",
LogLevel.Warning => "warn",
LogLevel.Error => "fail",
LogLevel.Critical => "crit",
_ => "unknown"
}
}); Alternative DesignsWe can probably use a Risks
|
Background and Motivation
I like to use the built-in console logging in production because it is easy and it looks good. The thing that frustrates me when using it is its log level string behaviour. Namely we take the source code as example:
runtime/src/libraries/Microsoft.Extensions.Logging.Console/src/SimpleConsoleFormatter.cs
Line 134 in 3c801a8
This is hardcoded and, somehow, not as common as things like "DEBUG", "ERROR", etc. For example, when I was using LogDNA, which should automatically parse log and log levels, cannot infer "fail" because it thinks this pattern is not common.
It would be very helpful to make this customisable.
Proposed API
I don't know, probably adding a configuration to
SimpleConsoleFormatterOptions
is simple enough:namespace Microsoft.Extensions.Logging.Console { public class SimpleConsoleFormatterOptions : ConsoleFormatterOptions { public SimpleConsoleFormatterOptions() { } public LoggerColorBehavior ColorBehavior { get; set; } public bool SingleLine { get; set; } + public Func<LogLevel, string> LogLevelStringFormatter { get; set; } } }
If the user does not set this, we can use the default strings as they are now.
Usage Examples
Like this:
Alternative Designs
We can probably use a
Dictionary<LogLevel, string>
, but we cannot guarantee all types are covered?Risks
The text was updated successfully, but these errors were encountered: