Best way to enable ansi formatting for terminal output only #2640
-
I have an application using tracing_subscriber::fmt::init(); Have noticed by default this enables ansi formatting, which has formatting issues when redirecting output to a file or running as a systemd service. I came up with the below function to do basically the equivalent of above but only enable ansi formatting if Is there a simpler way to do what is below? Could something similar to below be considered for the default Thanks! fn initialize_tracing_subscriber() {
use tracing_subscriber::{filter::LevelFilter, fmt, prelude::*, EnvFilter};
use std::io::IsTerminal;
let use_ansi = std::io::stdout().is_terminal();
let env_filter = EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy();
tracing_subscriber::registry()
.with(env_filter)
.with(fmt::layer().with_ansi(use_ansi))
.init();
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Sorry for the delay. What you wrote is generally the correct approach, and while we'd like to make |
Beta Was this translation helpful? Give feedback.
Sorry for the delay. What you wrote is generally the correct approach, and while we'd like to make
tracing_subscriber::fmt
callstd::io::stdout().is_terminal()
, we can't due to our current MSRV.