Skip to content

0.5.8

Choose a tag to compare

@KodrAus KodrAus released this 18 Apr 03:15
afd147a

Key Changes

  • Support setting default values for environment variables before initialising a logger

Contributions

More Details

Env::filter_or and Env::write_style_or

Default values for environment variables can be specified on the Env type using the filter_or and write_style_or methods. The default value is a string in the same format as the environment variable:

impl Env {
    fn filter_or<E, V>(self, filter_env: E, default: V) -> Self
    where
        E: Into<Cow<'a, str>>,
        V: Into<Cow<'a, str>>;

    fn write_style_or<E, V>(self, write_style_env: E, default: V) -> Self
        where
            E: Into<Cow<'a, str>>,
            V: Into<Cow<'a, str>>;
}

In usage:

let env = Env::default()
    .filter_or("MY_LOG_LEVEL", "trace")
    .write_style_or("MY_LOG_STYLE", "always");

env_logger::init_from_env(env);

DEFAULT_FILTER_ENV and DEFAULT_WRITE_STYLE_ENV

Two new constants are publicly exported that correspond to the default environment variable names used for the filters and style preferences.