Skip to content

Commit

Permalink
feat: add TargetFilter::level_for_not (#47)
Browse files Browse the repository at this point in the history
* feat: add TargetFilter::level_for_not

* fix
  • Loading branch information
andylokandy authored Aug 14, 2024
1 parent 4ebac33 commit 8fd1f34
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/filter/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,37 @@ use crate::filter::FilterResult;

/// A filter that checks if the log level is higher than the specified level for a specific
/// target.
///
/// Only if the target has a prefix that matches the target of the log record, the filter
/// will be applied.
#[derive(Debug, Clone)]
pub struct TargetFilter {
target: Cow<'static, str>,
level: log::LevelFilter,
not: bool,
}

impl TargetFilter {
/// The filter will be applied only if the target **has** a prefix that matches the target of
/// the log record.
pub fn level_for(target: impl Into<Cow<'static, str>>, level: log::LevelFilter) -> Self {
TargetFilter {
target: target.into(),
level,
not: false,
}
}

/// The filter will be applied only if the target **does not have** a prefix that matches the
/// target of the log record,
pub fn level_for_not(target: impl Into<Cow<'static, str>>, level: log::LevelFilter) -> Self {
TargetFilter {
target: target.into(),
level,
not: true,
}
}

pub(crate) fn filter(&self, metadata: &Metadata) -> FilterResult {
if metadata.target().starts_with(self.target.as_ref()) {
let matched = metadata.target().starts_with(self.target.as_ref());
if (matched && !self.not) || (!matched && self.not) {
let level = metadata.level();
if level <= self.level {
FilterResult::Neutral
Expand Down

0 comments on commit 8fd1f34

Please sign in to comment.