Skip to content

Commit

Permalink
feat: add target filter (#44)
Browse files Browse the repository at this point in the history
Co-authored-by: tison <[email protected]>
  • Loading branch information
andylokandy and tisonkun committed Aug 13, 2024
1 parent 53810bd commit a434e9e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/filter/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use log::Metadata;
use crate::filter::Filter;
use crate::filter::FilterResult;

/// A filter that checks if the log level is at most the specified level.
/// A filter that checks if the log level is higher than the specified level.
///
/// From least to most verbose, the levels are:
///
Expand All @@ -31,9 +31,13 @@ use crate::filter::FilterResult;
///
/// If LevelFilter is set to `Off`, it will reject all logs.
#[derive(Debug, Clone)]
pub struct LevelFilter(pub log::LevelFilter);
pub struct LevelFilter(log::LevelFilter);

impl LevelFilter {
pub fn new(level: log::LevelFilter) -> Self {
LevelFilter(level)
}

pub(crate) fn filter(&self, metadata: &Metadata) -> FilterResult {
let level = metadata.level();
if level <= self.0 {
Expand Down
4 changes: 4 additions & 0 deletions src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

pub use self::custom::CustomFilter;
pub use self::level::LevelFilter;
pub use self::target::TargetFilter;

mod custom;
mod level;
mod target;

/// The result of a filter may return.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand All @@ -34,13 +36,15 @@ pub enum FilterResult {
#[derive(Debug)]
pub enum Filter {
Level(LevelFilter),
Target(TargetFilter),
Custom(CustomFilter),
}

impl Filter {
pub(crate) fn filter(&self, metadata: &log::Metadata) -> FilterResult {
match self {
Filter::Level(filter) => filter.filter(metadata),
Filter::Target(filter) => filter.filter(metadata),
Filter::Custom(filter) => filter.filter(metadata),
}
}
Expand Down
59 changes: 59 additions & 0 deletions src/filter/target.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2024 FastLabs Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::borrow::Cow;

use log::Metadata;

use crate::filter::Filter;
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,
}

impl TargetFilter {
pub fn level_for(target: impl Into<Cow<'static, str>>, level: log::LevelFilter) -> Self {
TargetFilter {
target: target.into(),
level,
}
}

pub(crate) fn filter(&self, metadata: &Metadata) -> FilterResult {
if metadata.target().starts_with(self.target.as_ref()) {
let level = metadata.level();
if level <= self.level {
FilterResult::Neutral
} else {
FilterResult::Reject
}
} else {
FilterResult::Neutral
}
}
}

impl From<TargetFilter> for Filter {
fn from(filter: TargetFilter) -> Self {
Filter::Target(filter)
}
}

0 comments on commit a434e9e

Please sign in to comment.