diff --git a/src/filter/env.rs b/src/filter/env.rs index 1bca60e..b11d692 100644 --- a/src/filter/env.rs +++ b/src/filter/env.rs @@ -155,9 +155,13 @@ impl EnvFilterBuilder { EnvFilterBuilder(env_filter::Builder::new()) } - /// Initializes the filter builder from an environment. - pub fn from_env(env: &str) -> Self { - EnvFilterBuilder(env_filter::Builder::from_env(env)) + /// Try to initialize the filter builder from an environment; return `None` if the environment + /// variable is not set or invalid. + pub fn try_from_env(env: &str) -> Option { + let mut builder = env_filter::Builder::new(); + let config = std::env::var(env).ok()?; + builder.try_parse(&config).ok()?; + Some(EnvFilterBuilder(builder)) } /// Adds a directive to the filter for a specific module.