pub struct Env<'a> { /* private fields */ }
Expand description
Set of environment variables to configure from.
§Default environment variables
By default, the Env
will read the following environment variables:
RUST_LOG
: the level filterRUST_LOG_STYLE
: whether or not to print styles with records.
These sources can be configured using the builder methods on Env
.
Implementations§
Source§impl<'a> Env<'a>
impl<'a> Env<'a>
Sourcepub fn filter<E>(self, filter_env: E) -> Self
pub fn filter<E>(self, filter_env: E) -> Self
Specify an environment variable to read the filter from.
Examples found in repository?
More examples
26 fn init_logger() {
27 let env = Env::default()
28 .filter("MY_LOG_LEVEL")
29 .write_style("MY_LOG_STYLE");
30
31 Builder::from_env(env)
32 .format(|buf, record| {
33 // We are reusing `anstyle` but there are `anstyle-*` crates to adapt it to your
34 // preferred styling crate.
35 let warn_style = buf.default_level_style(log::Level::Warn);
36 let timestamp = buf.timestamp();
37
38 writeln!(
39 buf,
40 "My formatted log ({timestamp}): {warn_style}{}{warn_style:#}",
41 record.args()
42 )
43 })
44 .init();
45 }
Sourcepub fn filter_or<E, V>(self, filter_env: E, default: V) -> Self
pub fn filter_or<E, V>(self, filter_env: E, default: V) -> Self
Specify an environment variable to read the filter from.
If the variable is not set, the default value will be used.
Examples found in repository?
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}
Sourcepub fn default_filter_or<V>(self, default: V) -> Self
pub fn default_filter_or<V>(self, default: V) -> Self
Use the default environment variable to read the filter from.
If the variable is not set, the default value will be used.
Sourcepub fn write_style<E>(self, write_style_env: E) -> Self
pub fn write_style<E>(self, write_style_env: E) -> Self
Specify an environment variable to read the style from.
Examples found in repository?
More examples
26 fn init_logger() {
27 let env = Env::default()
28 .filter("MY_LOG_LEVEL")
29 .write_style("MY_LOG_STYLE");
30
31 Builder::from_env(env)
32 .format(|buf, record| {
33 // We are reusing `anstyle` but there are `anstyle-*` crates to adapt it to your
34 // preferred styling crate.
35 let warn_style = buf.default_level_style(log::Level::Warn);
36 let timestamp = buf.timestamp();
37
38 writeln!(
39 buf,
40 "My formatted log ({timestamp}): {warn_style}{}{warn_style:#}",
41 record.args()
42 )
43 })
44 .init();
45 }
Sourcepub fn write_style_or<E, V>(self, write_style_env: E, default: V) -> Self
pub fn write_style_or<E, V>(self, write_style_env: E, default: V) -> Self
Specify an environment variable to read the style from.
If the variable is not set, the default value will be used.
Examples found in repository?
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}
Sourcepub fn default_write_style_or<V>(self, default: V) -> Self
pub fn default_write_style_or<V>(self, default: V) -> Self
Use the default environment variable to read the style from.
If the variable is not set, the default value will be used.