Skip to content

Commit

Permalink
change log filter/ModuleFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Mar 9, 2024
1 parent c64d84c commit b430510
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 65 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
Expand Down
20 changes: 14 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,7 +22,7 @@ pub struct Config {
/// the log level filter
pub level: LevelFilter,
/// filter log
pub filter: Box<dyn Filter>,
pub filters: SyncVec<Box<dyn Filter>>,
/// format record into field fast_log_record's formated:String
pub format: Box<dyn RecordFormat>,
/// the channel length,default None(Unbounded channel)
Expand All @@ -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,
}
Expand All @@ -61,9 +61,17 @@ impl Config {
self.level = level;
self
}
/// set log Filter
pub fn filter<F: Filter + 'static>(mut self, filter: F) -> Self {
self.filter = Box::new(filter);
/// add log Filter
pub fn add_filter<F: Filter + 'static>(self, filter: F) -> Self {
self.filters.push(Box::new(filter));
self
}

/// add log Filter
pub fn filter(self, filters: Vec<Box<dyn Filter>>) -> Self {
for x in filters {
self.filters.push(x);
}
self
}
/// set log format
Expand Down
27 changes: 15 additions & 12 deletions src/fast_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});
}
}
}
Expand Down
71 changes: 25 additions & 46 deletions src/filter.rs
Original file line number Diff line number Diff line change
@@ -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<Vec<String>>,
//exclude contains
pub exclude: Option<Vec<String>>,
pub modules: SyncVec<String>,
}

impl ModuleFilter {
pub fn new_include(arg: Vec<String>) -> Self {
Self {
include: Some(arg),
exclude: None,
}
pub fn new() -> Self {
Self { modules: SyncVec::new() }
}
pub fn new_exclude(arg: Vec<String>) -> Self {
Self {
include: None,
exclude: Some(arg),
}
}
pub fn new(include: Option<Vec<String>>, exclude: Option<Vec<String>>) -> 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;
}
}
25 changes: 25 additions & 0 deletions tests/filter_test.rs
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit b430510

Please sign in to comment.