Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add TargetFilter::level_for_not #47

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read challenging. IIRC it could be generally neg_level_for or negtive_level_for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not negative on level but negative on for. So I'd like to put not word after for.

Copy link
Contributor Author

@andylokandy andylokandy Aug 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe level_excluding, level_for_excluding?

Copy link
Contributor Author

@andylokandy andylokandy Aug 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In practice, it is like:

let dispatch = Dispatch::new()
    // discard other targets
    .filter(TargetFilter::level_for_not(
        "databend::log::structlog",
        LevelFilter::Off,
    ))
    .append(structlog_log_file);
logger = logger.dispatch(dispatch);

Copy link
Contributor

@tisonkun tisonkun Aug 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to:

pub fn negative(mut self, negative: bool) -> Self {
  self.negative = negative;
  self
}

Maybe name it not, I'm OK with it. But the pattern is to invert the matching result manually in a fluent method. What do you think?

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
Expand Down
Loading