pretty-env-logger icon indicating copy to clipboard operation
pretty-env-logger copied to clipboard

Initialize env_logger::Builder through env_logger::from_env

Open Luro02 opened this issue 3 years ago • 2 comments

Executing this code

use log::{trace, debug, info, warn, error};

fn main() {
    pretty_env_logger::formatted_builder().init();

    trace!("a trace example");
    debug!("deboogging");
    info!("such information");
    warn!("o_O");
    error!("boom");
}

with

RUST_LOG=trace cargo run

prints

 ERROR terminal_image > boom

and this is quite confusing, because I expected it to print all log levels (it ignores the environment variable).

Luro02 avatar Aug 24 '20 08:08 Luro02

The docs with this are pretty minimal, and some of them refer to the wrong project (ie //https://docs.rs/env_logger/0.5.0-rc.1/env_logger/struct.Builder.html). I don't think formatted_builder() works in this crate. Try this:

     if env::var_os("RUST_LOG").is_none() {                                                                                                                                                                                                                                      
       // Set `RUST_LOG=myapp=debug` to see debug logs, this only shows info logs.                                                                                                                                                                                       
       env::set_var("RUST_LOG", "myapp=info");                                                                                                                                                                                                                             
     }                                                                                                                                                                                                                                                                           
     pretty_env_logger::init();

mabushey avatar Aug 27 '20 15:08 mabushey

Also, instead of use log::{trace, debug, info, warn, error};, you can use #[macro_use] extern crate log;

mabushey avatar Aug 27 '20 15:08 mabushey