rust-cli / env_logger

A logging implementation for `log` which is configured via an environment variable.
https://docs.rs/env_logger
Apache License 2.0
797 stars 124 forks source link

No color on Windows when using builder #225

Closed roman-khazanskii closed 6 months ago

roman-khazanskii commented 2 years ago

If I just do env_logger::init(); then I see colored log messages, as expected, with different color for every log leve.

However, if I use builder - everything is colorless, even though I call .write_style(WriteStyle::Always):

    env_logger::builder()
        .format(|buf, record| {
            writeln!(
                buf,
                "{} [{}] - {}",
                Local::now().format("%Y-%m-%d %H:%M:%S"),
                record.level(),
                record.args()
            )
        })
        .write_style(WriteStyle::Always)
        .filter(Some(botname), LevelFilter::Info)
        .init();

However, if I manually color all the text, like this:

    env_logger::builder()
        .format(|buf, record| {

            let mut style = buf.style();

            style.set_color(Color::Red);

            writeln!(
                buf,
                "{} [{}] - {}",
                Local::now().format("%Y-%m-%d %H:%M:%S"),
                record.level(),
                style.value(record.args())
            )
        })
        .filter(Some(botname), LevelFilter::Info)
        .write_style(WriteStyle::Always)
        .init();

red text is printed. Does it mean that if I want to use builder, I have to manually color all different levels?

relaxcn commented 2 years ago

Me too. Does anyone have any idea?

relaxcn commented 2 years ago

It would be better if you could set the time zone

lmapii commented 2 years ago

Stumbled upon the same topic. Once you overwrite . format you essentially use all the styling options and most of the builder calls won't do any good anymore. Same holds for pretty_env_logger. The fact that the format: fmt::Builder member of the Builder is private makes all additional builder calls obsolete. It would be great to have access to format within the closure?

Timmmm commented 1 year ago

I have this problem even without format(). I have this code for tests:

    let _ = env_logger::builder().filter_level(log::LevelFilter::Info).write_style(env_logger::WriteStyle::Always).is_test(true).try_init();
    error!("Error!!");

No colour on Windows (haven't tried other platforms).

Timmmm commented 1 year ago

Ah actually in my case it was is_test(true) that was causing the lack of colour. Without that I get colour. Not exactly sure why it would do that but never mind.

epage commented 7 months ago

298 will switch us to anstream and allow WriteStyle to control is_test output

epage commented 6 months ago

At this point, users have all they need for this.

If you override our formatting, you have to provide the styling on your own. With 0.11, you can use any ANSI escape code formatting library you want and env_logger will automatically adapt it to the terminal you are using. We provide default_level_style to help you be consistent with the colors we use.