pub fn init_from_env<'a, E>(env: E)
Expand description
Initializes the global logger with an env logger from the given environment variables.
This should be called early in the execution of a Rust program. Any log events that occur before initialization will be ignored.
§Examples
Initialise a logger using the MY_LOG
environment variable for filters
and MY_LOG_STYLE
for writing colors:
use env_logger::{Builder, Env};
let env = Env::new().filter("MY_LOG").write_style("MY_LOG_STYLE");
env_logger::init_from_env(env);
§Panics
This function will panic if it is called more than once, or if another library has already initialized a global logger.
Examples found in repository?
examples/default.rs (line 30)
22fn main() {
23 // The `Env` lets us tweak what the environment
24 // variables to read are and what the default
25 // value is if they're missing
26 let env = Env::default()
27 .filter_or("MY_LOG_LEVEL", "trace")
28 .write_style_or("MY_LOG_STYLE", "always");
29
30 env_logger::init_from_env(env);
31
32 trace!("some trace log");
33 debug!("some debug log");
34 info!("some information log");
35 warn!("some warning log");
36 error!("some error log");
37}