How to avoid text coloring when using tracing-appender?

2.7k Views Asked by At

When I write logs to a file with tracing-appender, I get output with terminal color artifacts which do not render when viewing them as a text file:

[2mOct 02 23:44:57.484[0m [34mDEBUG[0m

Is there a way to drop these artifacts?

2

There are 2 best solutions below

0
On BEST ANSWER

The with_ansi() option is on by default when using tracing_subcriber::fmt(). Set it to false by using the builder method to declare your subscriber:

let (non_blocking, _guard) = tracing_appender::non_blocking(TestWriter);

tracing_subscriber::fmt()
    .with_writer(non_blocking)
    .with_ansi(false) // <------ this
    .init();

You can have multiple subscribers if you want to inject color on the terminal but not in the file.

0
On

By the way, you can use like this if you are using axum with tracing_subscriber:

tracing_subscriber::registry()
  .with(tracing_subscriber::fmt::layer().with_ansi(false))
  .init();