-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
80 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]>"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |