pretty-env-logger
pretty-env-logger copied to clipboard
Initialize env_logger::Builder through env_logger::from_env
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).
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();
Also, instead of use log::{trace, debug, info, warn, error};
, you can use #[macro_use] extern crate log;