serilog / serilog-sinks-console

Write log events to System.Console as text or JSON, with ANSI theme support
Apache License 2.0
248 stars 72 forks source link

Please support NO_COLOR environment variable #153

Closed borland closed 8 months ago

borland commented 8 months ago

We use Serilog directly in a number of systems, and indirectly via some Nuke build scripts -- Nuke uses Serilog itself.

We enjoy colored console logging when running interactively, but this same code is often run in Automated Builds (TeamCity, GitHub actions, etc) where stdout is captured by the build system - sometimes indirectly through docker run or docker compose.

This leads to messy log files full of escape sequences such as this:

[38;5;247m[ [0m [38;5;45;1mINF [0m [38;5;247m]  [0m [39mDocker  [0m [38;5;45;1mStdOut [0m [39m:  [0m [38;5;45;1mrunning [0m

I am aware that this package supports ConsoleTheme.None to disable colorization, but we do not want to alter our code (or in the case of Nuke, cannot directly alter it), as then we would lose the nice coloring when running interactively on developer systems.

There are also important accessibility and compatibility reasons that people want to disable color. Colorblindness, or colors that conflict with terminal themes, etc.

The NO_COLOR=1 environment variable has emerged as a de-facto standard for this, and is supported by a wide range of software. More information at https://no-color.org/

To report a bug or request a feature, please give us as much information as possible, for example:

  • the exact package id and version you're using, Serilog.Sinks.Console 5.0.1 - latest on Nuget.org at time of writing Serilog.3.1.1 - latest on Nuget.org at time of writing

  • your dotnet toolchain version, target framework, and operating system, ` dotnet SDK 8.0.101, target framework net8.0, Windows 11

  • the current behavior, and

Build and run this small C# sample application.

using Serilog;

var log = new LoggerConfiguration()
    .WriteTo.Console()
    .CreateLogger();

log.Information("Hello, {Name}!", Environment.UserName);
log.Warning("Today is {Today} and the number of the day is {Number}", DateTime.Now, 12);

what you expect or want to happen instead.

When executed with the NO_COLOR env var set to 1, I expect to see no colors printed. But: I see colorized output image

Thanks!

nblumhardt commented 8 months ago

Thanks for the suggestion!

Should NO_COLOR apply even if a theme is set explicitly?

borland commented 8 months ago

I would suggest that it should override even an explicitly set theme. A setting in code tends to be product/team wide whereas an environment variable tends to be scoped to the machine/container. More specific wins over less specific, if that makes sense. Thanks!

nblumhardt commented 8 months ago

Sounds good to me, should be fairly straightforward to implement directly in the WriteTo.Console method, by switching the theme when the variable is detected.

Anyone following along keen to take a shot at a PR?

lonix1 commented 3 days ago

If one is unable to set the NO_COLOR env var, then there's another way:

.WriteTo.Console(
  #if DEBUG
  theme: AnsiConsoleTheme.Code
  #else
  theme: AnsiConsoleTheme.None
  #endif
)

For JSON config, one could use an appsettings.json and appsettings.Development.json pair.