From e8c9b937f8611cce70c8b5aa2d08f3d9792de682 Mon Sep 17 00:00:00 2001 From: andylokandy Date: Wed, 14 Aug 2024 16:38:37 +0800 Subject: [PATCH 1/2] feat: add TargetFilter::level_for_not --- 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..cab46a3 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 mathced = metadata.target().starts_with(self.target.as_ref()); + if (mathced && !self.not) || (!mathced && self.not) { let level = metadata.level(); if level <= self.level { FilterResult::Neutral From ee91023e666653d49d661930989d2e08a02dc2f1 Mon Sep 17 00:00:00 2001 From: andylokandy Date: Wed, 14 Aug 2024 17:21:08 +0800 Subject: [PATCH 2/2] fix --- src/filter/target.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/filter/target.rs b/src/filter/target.rs index cab46a3..e3f223d 100644 --- a/src/filter/target.rs +++ b/src/filter/target.rs @@ -50,8 +50,8 @@ impl TargetFilter { } pub(crate) fn filter(&self, metadata: &Metadata) -> FilterResult { - let mathced = metadata.target().starts_with(self.target.as_ref()); - if (mathced && !self.not) || (!mathced && self.not) { + 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