4

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?

kmdreko
  • 42,554
  • 6
  • 57
  • 106

2 Answers2

9

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.

kmdreko
  • 42,554
  • 6
  • 57
  • 106
Ahmed Masud
  • 21,655
  • 3
  • 33
  • 58
1

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();
Arco
  • 614
  • 5
  • 13