simplelog.rs icon indicating copy to clipboard operation
simplelog.rs copied to clipboard

TermLogger emits colors when stdout is not a TTY

Open kartva opened this issue 2 years ago • 2 comments

use simplelog::{TermLogger, Config};
use log::*;

fn main() {
    TermLogger::init(LevelFilter::Debug, Config::default(), simplelog::TerminalMode::Stdout, simplelog::ColorChoice::Auto).unwrap();
    log::info!("hello");
}

running

cargo run > test.txt

results in ANSI output being written to test.txt.

kartva avatar Jul 21 '22 17:07 kartva

This appears to be an issue in termcolor library that simplelog uses.

kartva avatar Jul 21 '22 17:07 kartva

termcolor explicitly states that it makes no effort to check whether the stream it's printing to is a TTY or not. It recommends atty for the same. supports_color is a wrapper over atty that also checks the NO_COLOR env variable.

kartva avatar Jul 21 '22 17:07 kartva