Last active
November 30, 2018 17:59
-
-
Save jonas-schievink/70337487b05ca63fc4d64f44d972829c to your computer and use it in GitHub Desktop.
Default log level for (pretty_)env_logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate env_logger; | |
extern crate log; | |
use std::env; | |
fn main() { | |
// By default, log all `info!` messages and higher, in all modules and crates. | |
// Can be overridden by specifying the `RUST_LOG` environment variable. | |
if env::var_os("RUST_LOG").is_none() { | |
env_logger::Builder::from_default_env() | |
.filter(None, log::LevelFilter::Info) | |
.init(); | |
} else { | |
env_logger::init(); | |
} | |
info!("shows up without having to set RUST_LOG"); | |
debug!("only shows up when RUST_LOG=debug is set"); | |
// Or: Log *all levels* for the current crate (`module_path!()` will expand to the crate name) | |
if env::var_os("RUST_LOG").is_none() { | |
pretty_env_logger::formatted_builder() | |
.unwrap() | |
.filter(Some(module_path!()), log::LevelFilter::Trace) | |
.init(); | |
} else { | |
pretty_env_logger::init(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment