From b43051059f47b005a827d9e70c7c472cadd55529 Mon Sep 17 00:00:00 2001 From: "zhuxiujia@qq.com" Date: Sat, 9 Mar 2024 23:39:52 +0800 Subject: [PATCH] change log filter/ModuleFilter --- Cargo.toml | 2 +- src/config.rs | 20 +++++++++---- src/fast_log.rs | 27 +++++++++-------- src/filter.rs | 71 ++++++++++++++++---------------------------- tests/filter_test.rs | 25 ++++++++++++++++ 5 files changed, 80 insertions(+), 65 deletions(-) create mode 100644 tests/filter_test.rs diff --git a/Cargo.toml b/Cargo.toml index ba52350..2a30b5f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ members = [ [package] name = "fast_log" -version = "1.6.12" +version = "1.6.13" description = "Rust async log High-performance asynchronous logging" readme = "Readme.md" authors = ["ce "] diff --git a/src/config.rs b/src/config.rs index 8bab5a1..295191a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,6 @@ use crate::appender::{LogAppender, RecordFormat}; use crate::consts::LogSize; -use crate::filter::{Filter, NoFilter}; +use crate::filter::{Filter}; use crate::plugin::console::ConsoleAppender; use crate::plugin::file::FileAppender; use crate::plugin::file_loop::FileLoopAppender; @@ -22,7 +22,7 @@ pub struct Config { /// the log level filter pub level: LevelFilter, /// filter log - pub filter: Box, + pub filters: SyncVec>, /// format record into field fast_log_record's formated:String pub format: Box, /// the channel length,default None(Unbounded channel) @@ -44,7 +44,7 @@ impl Default for Config { Self { appends: SyncVec::new(), level: LevelFilter::Trace, - filter: Box::new(NoFilter {}), + filters: SyncVec::new(), format: Box::new(FastLogFormat::new()), chan_len: None, } @@ -61,9 +61,17 @@ impl Config { self.level = level; self } - /// set log Filter - pub fn filter(mut self, filter: F) -> Self { - self.filter = Box::new(filter); + /// add log Filter + pub fn add_filter(self, filter: F) -> Self { + self.filters.push(Box::new(filter)); + self + } + + /// add log Filter + pub fn filter(self, filters: Vec>) -> Self { + for x in filters { + self.filters.push(x); + } self } /// set log format diff --git a/src/fast_log.rs b/src/fast_log.rs index 9aaabbd..dfed9de 100644 --- a/src/fast_log.rs +++ b/src/fast_log.rs @@ -62,19 +62,22 @@ impl Log for Logger { fn log(&self, record: &Record) { if let Some(filter) = LOGGER.cfg.get() { if let Some(send) = LOGGER.send.get() { - if !filter.filter.filter(record) { - let _ = send.send(FastLogRecord { - command: Command::CommandRecord, - level: record.level(), - target: record.metadata().target().to_string(), - args: record.args().to_string(), - module_path: record.module_path().unwrap_or_default().to_string(), - file: record.file().unwrap_or_default().to_string(), - line: record.line().clone(), - now: SystemTime::now(), - formated: String::new(), - }); + for filter in filter.filters.iter() { + if !filter.do_log(record) { + return; + } } + let _ = send.send(FastLogRecord { + command: Command::CommandRecord, + level: record.level(), + target: record.metadata().target().to_string(), + args: record.args().to_string(), + module_path: record.module_path().unwrap_or_default().to_string(), + file: record.file().unwrap_or_default().to_string(), + line: record.line().clone(), + now: SystemTime::now(), + formated: String::new(), + }); } } } diff --git a/src/filter.rs b/src/filter.rs index 1e11832..0ae9c95 100644 --- a/src/filter.rs +++ b/src/filter.rs @@ -1,66 +1,45 @@ +use dark_std::sync::SyncVec; + ///log filter pub trait Filter: Send + Sync { - //return is filter - fn filter(&self, record: &log::Record) -> bool; -} - -pub struct NoFilter {} - -impl Filter for NoFilter { - fn filter(&self, _module: &log::Record) -> bool { - return false; - } + /// if return true=do_log/false=not_log + fn do_log(&self, record: &log::Record) -> bool; } +/// an Module Filter +/// ```rust +/// fn main(){ +/// use fast_log::Config; +/// use fast_log::filter::ModuleFilter; +/// let filter = ModuleFilter::new(); +/// filter.add(module_path!()); +/// fast_log::init(Config::new().console().add_filter(filter)).unwrap(); +/// } +/// ``` pub struct ModuleFilter { - //include contains - pub include: Option>, - //exclude contains - pub exclude: Option>, + pub modules: SyncVec, } impl ModuleFilter { - pub fn new_include(arg: Vec) -> Self { - Self { - include: Some(arg), - exclude: None, - } + pub fn new() -> Self { + Self { modules: SyncVec::new() } } - pub fn new_exclude(arg: Vec) -> Self { - Self { - include: None, - exclude: Some(arg), - } - } - pub fn new(include: Option>, exclude: Option>) -> Self { - Self { include, exclude } + + pub fn add(&self, module: &str) { + self.modules.push(module.to_string()); } } impl Filter for ModuleFilter { - fn filter(&self, record: &log::Record) -> bool { + fn do_log(&self, record: &log::Record) -> bool { let module = record.module_path().unwrap_or(""); - if self.include.is_some() { - for x in self.include.as_ref().unwrap() { - if module.contains(x) { - //not filter + if !self.modules.is_empty() { + for x in &self.modules { + if module == x { return false; } } - //filter - return true; - } - if self.exclude.is_some() { - for x in self.exclude.as_ref().unwrap() { - if module.contains(x) { - //filter - return true; - } - } - //not filter - return false; } - //not filter - return false; + return true; } } diff --git a/tests/filter_test.rs b/tests/filter_test.rs new file mode 100644 index 0000000..df1ce06 --- /dev/null +++ b/tests/filter_test.rs @@ -0,0 +1,25 @@ +#[cfg(test)] +mod test { + use log::LevelFilter; + use fast_log::{Config, FastLogFormat}; + use fast_log::appender::{FastLogRecord, LogAppender}; + use fast_log::filter::ModuleFilter; + + #[test] + fn test_send_pack() { + let m = ModuleFilter::new(); + m.add( module_path!()); + pub struct A{} + impl LogAppender for A{ + fn do_logs(&self, _records: &[FastLogRecord]) { + panic!("must be filter log,but do_log"); + } + } + fast_log::init(Config::new() + .console() + .format(FastLogFormat::new().set_display_line_level(LevelFilter::Trace)) + .add_filter(m)).unwrap(); + log::info!("aaa"); + log::logger().flush(); + } +}