yingmanwumen
yingmanwumen

Reputation: 119

Why the env_logger doesn't work in my cli program

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?

Upvotes: 4

Views: 3692

Answers (1)

yingmanwumen
yingmanwumen

Reputation: 119

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

So I should write

export RUST_LOG=grrs=info

Upvotes: 1

Related Questions