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

Adds a way to filter logs by module name #4

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 19 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ pub struct Log2 {
filesize: u64,
count: usize,
level: String,
module_filter: Option<Box<dyn Fn(&str) -> bool + Send>>,
}

struct Context {
Expand Down Expand Up @@ -182,6 +183,7 @@ impl Log2 {
filesize: 100 * 1024 * 1024,
count: 10,
level: String::new(),
module_filter: None,
}
}

Expand Down Expand Up @@ -215,6 +217,12 @@ impl Log2 {
self
}

/// provide a way to filter by module
pub fn module_filter(mut self, filter: impl Fn(&str) -> bool + Send + 'static) -> Log2 {
self.module_filter = Some(Box::new(filter));
self
}

pub fn level<T: fmt::Display>(mut self, name: T) -> Self {
self.level = name.to_string();
self
Expand All @@ -240,25 +248,19 @@ impl log::Log for Log2 {
}

fn log(&self, record: &Record) {
// cheap way to ignore other crates with absolute files (UNIX)
// TODO: filter by crate/module name?
let file = record.file().unwrap_or("unknown");
if file.starts_with("/") {
return;
let module = record.module_path().unwrap_or("unknown");

// module filter
if let Some(filter) = &self.module_filter {
if !filter(module) {
return;
}
}

// module
let mut module = "".into();
let mut origin = record.file().unwrap_or("unknown").to_string();
if self.module {
if file.starts_with("src/") {
if file.ends_with("/mod.rs") {
module = format!("{}: ", &file[4..file.len() - 7]);
} else if file.ends_with(".rs") {
module = format!("{}: ", &file[4..file.len() - 3]);
}
} else {
module = format!("{file}: ");
}
origin = format!("[{}]", module);
}

// stdout
Expand All @@ -267,7 +269,7 @@ impl log::Log for Log2 {
let open = "[".truecolor(0x87, 0x87, 0x87);
let close = "]".truecolor(0x87, 0x87, 0x87);
let line = format!(
"{open}{}{close} {open}{}{close} {module}{}",
"{open}{}{close} {open}{}{close} {origin} {}",
Local::now().format("%Y-%m-%d %H:%M:%S%.3f"),
level,
record.args()
Expand All @@ -278,7 +280,7 @@ impl log::Log for Log2 {
// file
if self.path.len() > 0 {
let line = format!(
"[{}] [{}] {module}{}\n",
"[{}] [{}] {origin} {}\n",
Local::now().format("%Y-%m-%d %H:%M:%S%.3f"),
record.level(),
record.args()
Expand Down
37 changes: 37 additions & 0 deletions tests/log2_module_filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use log2::*;

#[test]
fn module_filter() {
let _log2 = Log2::new()
.tee(true)
// only log messages from modules that contain "first"
.module_filter(|module| module.contains("first"))
.start();

first_module::foo();
second_module::bar();

mod first_module {
use log2::*;

pub fn foo() {
trace!("send order request to server");
debug!("receive order response");
info!("order was executed");
warn!("network speed is slow");
error!("network connection was broken");
}
}

mod second_module {
use log2::*;

pub fn bar() {
trace!("send order request to server");
debug!("receive order response");
info!("order was executed");
warn!("network speed is slow");
error!("network connection was broken");
}
}
}