Skip to content

Commit

Permalink
add docs
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 46b0901 commit 4146256
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
48 changes: 48 additions & 0 deletions src/filter/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,56 @@ use crate::Filter;

const DEFAULT_FILTER_ENV: &str = "RUST_LOG";

/// A filter that respects the `RUST_LOG` environment variable.
///
/// Read [the `env_logger` documentation](https://docs.rs/env_logger/#enabling-logging) for more.
#[derive(Debug)]
pub struct EnvFilter(env_filter::Filter);

impl EnvFilter {
/// Initializes the filter builder from the environment using default variable name `RUST_LOG`.
///
/// # Examples
///
/// Initialize a filter using the default environment variables:
///
/// ```
/// use logforth::filter::EnvFilter;
/// let filter = EnvFilter::from_default_env();
/// ```
pub fn from_default_env() -> Self {
EnvFilter::from_env(DEFAULT_FILTER_ENV)
}

/// Initializes the filter builder from the environment using default variable name `RUST_LOG`.
/// If the variable is not set, the default value will be used.
///
/// # Examples
///
/// Initialize a filter using the default environment variables, or fallback to the default
/// value:
///
/// ```
/// use logforth::filter::EnvFilter;
/// let filter = EnvFilter::from_default_env_or("info");
/// ```
pub fn from_default_env_or<'a, V>(default: V) -> Self
where
V: Into<Cow<'a, str>>,
{
EnvFilter::from_env_or(DEFAULT_FILTER_ENV, default)
}

/// Initializes the filter builder from the environment using specific variable name.
///
/// # Examples
///
/// Initialize a filter using the using specific variable name:
///
/// ```
/// use logforth::filter::EnvFilter;
/// let filter = EnvFilter::from_env("MY_LOG");
/// ```
pub fn from_env<'a, E>(name: E) -> Self
where
E: Into<Cow<'a, str>>,
Expand All @@ -49,6 +84,18 @@ impl EnvFilter {
EnvFilter::new(builder)
}

/// Initializes the filter builder from the environment using specific variable name.
/// If the variable is not set, the default value will be used.
///
/// # Examples
///
/// Initialize a filter using the using specific variable name, or fallback to the default
/// value:
///
/// ```
/// use logforth::filter::EnvFilter;
/// let filter = EnvFilter::from_env_or("MY_LOG", "info");
/// ```
pub fn from_env_or<'a, 'b, E, V>(name: E, default: V) -> Self
where
E: Into<Cow<'a, str>>,
Expand All @@ -65,6 +112,7 @@ impl EnvFilter {
EnvFilter::new(builder)
}

/// Initializes the filter builder from the [EnvFilterBuilder].
pub fn new(mut builder: EnvFilterBuilder) -> Self {
EnvFilter(builder.build())
}
Expand Down
2 changes: 1 addition & 1 deletion src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub use self::target::TargetFilter;

mod custom;
#[cfg(feature = "env-filter")]
mod env;
pub mod env;
mod level;
mod target;

Expand Down

0 comments on commit 4146256

Please sign in to comment.