Function env_logger::init_from_env

source ·
pub fn init_from_env<'a, E>(env: E)
where E: Into<Env<'a>>,
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)
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
fn main() {
    // The `Env` lets us tweak what the environment
    // variables to read are and what the default
    // value is if they're missing
    let env = Env::default()
        .filter_or("MY_LOG_LEVEL", "trace")
        .write_style_or("MY_LOG_STYLE", "always");

    env_logger::init_from_env(env);

    trace!("some trace log");
    debug!("some debug log");
    info!("some information log");
    warn!("some warning log");
    error!("some error log");
}