From 8fd1f347422186123a086a42fad70d542f846aac Mon Sep 17 00:00:00 2001 From: Andy Lok Date: Wed, 14 Aug 2024 18:10:10 +0800 Subject: [PATCH] feat: add TargetFilter::level_for_not (#47) * feat: add TargetFilter::level_for_not * fix --- src/filter/target.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/filter/target.rs b/src/filter/target.rs index 4182f37..e3f223d 100644 --- a/src/filter/target.rs +++ b/src/filter/target.rs @@ -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>, 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>, 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