Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize env_logger::Builder through env_logger::from_env #41

Open
Luro02 opened this issue Aug 24, 2020 · 2 comments
Open

Initialize env_logger::Builder through env_logger::from_env #41

Luro02 opened this issue Aug 24, 2020 · 2 comments

Comments

@Luro02
Copy link

Luro02 commented Aug 24, 2020

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).

@mabushey
Copy link

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants