Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the env_logger doesn't work in my cli program

Tags:

rust

In Cargo.toml, I write this:

log = "0.4.0"
env_logger = "0.8.4"

In my test code, I write this:

use log::{info, warn};

fn main() {
    env_logger::init();
    info!("starting up");
    warn!("oops, nothing done");
}

And then I set the env like this:

$ env | grep RUST
RUST_LOG=output_log=info

Every thing was done, and the test code worked well:

$ cargo run --bin output-log
    Finished dev [unoptimized + debuginfo] target(s) in 0.04s
     Running `target/debug/output-log`
[2021-11-06T04:05:42Z INFO  output_log] starting up
[2021-11-06T04:05:42Z WARN  output_log] oops, nothing done

But When I did this in my program:

use log::*;
use std::fs::File;
use std::io::{self, BufRead, BufReader, BufWriter, Write};
use std::path::PathBuf;
...

fn main() -> Result<()> {
    let stdout = io::stdout();
    let mut stdout = BufWriter::new(stdout);

    env_logger::init();
    info!("This is info");
    warn!("This is warn");
    error!("This is error");
...
// write something to the stdout and flush
...
    Ok(())
}

Then, the env_logger didn't print any message to the terminal as before

Why this happens and what should I do to fix it?

like image 946
yingmanwumen Avatar asked Feb 03 '26 22:02

yingmanwumen


1 Answers

export RUST_LOG=`executable_name`=`log_level`,...

So I should write

export RUST_LOG=grrs=info
like image 70
yingmanwumen Avatar answered Feb 06 '26 13:02

yingmanwumen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!