Skip to content

Commit

Permalink
try_from_env
Browse files Browse the repository at this point in the history
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun committed Aug 16, 2024
1 parent 0f51555 commit a12c79b
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/filter/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
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.
Expand Down

0 comments on commit a12c79b

Please sign in to comment.