Search code examples
rustrust-tracing

How to avoid text coloring when using tracing-appender?


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?


Solution

  • 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.