From 8594ab8dba2c1fa7e9d684907cf3f643a45de124 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Tue, 20 Aug 2024 17:01:21 +0800 Subject: [PATCH] Add filtering to specified module logs --- Cargo.toml | 2 +- examples/filter_module.rs | 73 +++++++++++++++++++++++++++++++++++++++ src/lib.rs | 4 +++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 examples/filter_module.rs diff --git a/Cargo.toml b/Cargo.toml index 1e33706..54e0694 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace.package] -version = "1.7.2" +version = "1.7.3" edition = "2021" authors = ["baoyachi "] description = "A simple log. It's really simple use" diff --git a/examples/filter_module.rs b/examples/filter_module.rs new file mode 100644 index 0000000..85ce05c --- /dev/null +++ b/examples/filter_module.rs @@ -0,0 +1,73 @@ +//! `cargo run --example console` +//! +//! With OutPut +//! ```bash +//! 16:57:13.619320000 [DEBUG] :test console debug +//! 16:57:13.619481000 [INFO] :test console info +//! 16:57:13.619489000 [WARN] :test console warn +//! 16:57:13.619495000 [ERROR] :test console error +//! 16:57:13.619501000 [INFO] :init app +//! 16:57:13.619507000 [INFO] :init launch +//! 16:57:13.619515000 [DEBUG] :parser log + +//! ``` + +#[macro_use] +extern crate simple_log; + +use simple_log::LogConfig; + +fn main() -> Result<(), String> { + let config = r#" + level = "debug" + out_kind = "console" + time_format = "%H:%M:%S.%f" + filter_module = ["filter_module::app::ctrl","filter_module::app::launch::conf"] + "#; + let conf: LogConfig = toml::from_str(config).unwrap(); + + simple_log::new(conf).unwrap(); //init log + + debug!("test console debug"); + info!("test console info"); + warn!("test console warn"); + error!("test console error"); + + app::init_app(); + app::launch::init_launch(); + app::launch::conf::err_conf(); // this log filter + app::launch::parser::err_parser(); + app::ctrl::init_ctrl(); // this log filter + + Ok(()) +} + +pub(crate) mod app { + pub fn init_app() { + info!("init app") + } + + pub mod launch { + pub fn init_launch() { + info!("init launch") + } + + pub mod conf { + pub fn err_conf() { + error!("conf log") + } + } + + pub mod parser { + pub fn err_parser() { + debug!("parser log") + } + } + } + + pub mod ctrl { + pub fn init_ctrl() { + info!("init ctrl") + } + } +} diff --git a/src/lib.rs b/src/lib.rs index fdbcf57..56bfdf3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -251,12 +251,16 @@ pub fn get_log_conf() -> SimpleResult { #[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq, Eq)] pub struct LogConfig { + #[serde(default)] pub path: Option, pub level: String, + #[serde(default)] pub size: u64, #[serde(deserialize_with = "deserialize_out_kind", default)] pub out_kind: Vec, + #[serde(default)] pub roll_count: u32, + #[serde(default)] pub time_format: Option, #[serde(default)] pub filter_module: Vec,