Skip to content

Commit

Permalink
Merge pull request #37 from baoyachi/issue/27
Browse files Browse the repository at this point in the history
Add filtering to specified module logs
  • Loading branch information
baoyachi authored Aug 20, 2024
2 parents 190f25c + 8594ab8 commit 5073ab9
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace.package]
version = "1.7.2"
version = "1.7.3"
edition = "2021"
authors = ["baoyachi <[email protected]>"]
description = "A simple log. It's really simple use"
Expand Down
73 changes: 73 additions & 0 deletions examples/filter_module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! `cargo run --example console`
//!
//! With OutPut
//! ```bash
//! 16:57:13.619320000 [DEBUG] <filter_module:25>:test console debug
//! 16:57:13.619481000 [INFO] <filter_module:26>:test console info
//! 16:57:13.619489000 [WARN] <filter_module:27>:test console warn
//! 16:57:13.619495000 [ERROR] <filter_module:28>:test console error
//! 16:57:13.619501000 [INFO] <filter_module::app:41>:init app
//! 16:57:13.619507000 [INFO] <filter_module::app::launch:46>:init launch
//! 16:57:13.619515000 [DEBUG] <filter_module::app::launch::parser:57>: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")
}
}
}
20 changes: 19 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ mod out_kind;

use crate::out_kind::OutKind;
use convert_case::{Case, Casing};
use log::LevelFilter;
use log4rs::append::console::ConsoleAppender;
use log4rs::append::rolling_file::policy::compound::roll::fixed_window::FixedWindowRoller;
use log4rs::append::rolling_file::policy::compound::trigger::size::SizeTrigger;
use log4rs::append::rolling_file::policy::compound::CompoundPolicy;
use log4rs::append::rolling_file::RollingFileAppender;
use log4rs::config::{Appender, Config, Root};
use log4rs::config::runtime::LoggerBuilder;
use log4rs::config::{Appender, Config, Logger, Root};
use log4rs::encode::pattern::PatternEncoder;
use once_cell::sync::OnceCell;
use out_kind::deserialize_out_kind;
Expand Down Expand Up @@ -249,13 +251,19 @@ pub fn get_log_conf() -> SimpleResult<LogConfig> {

#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct LogConfig {
#[serde(default)]
pub path: Option<String>,
pub level: String,
#[serde(default)]
pub size: u64,
#[serde(deserialize_with = "deserialize_out_kind", default)]
pub out_kind: Vec<OutKind>,
#[serde(default)]
pub roll_count: u32,
#[serde(default)]
pub time_format: Option<String>,
#[serde(default)]
pub filter_module: Vec<String>,
}

impl LogConfig {
Expand Down Expand Up @@ -500,6 +508,7 @@ pub fn console<S: Into<String>>(level: S) -> SimpleResult<()> {
out_kind: vec![OutKind::Console],
roll_count: 0,
time_format: Some(DEFAULT_DATE_TIME_FORMAT.to_string()),
filter_module: vec![],
};
init_log_conf(config)?;
Ok(())
Expand Down Expand Up @@ -535,6 +544,7 @@ pub fn file<S: Into<String>>(path: S, level: S, size: u64, roll_count: u32) -> S
out_kind: vec![OutKind::File],
roll_count,
time_format: Some(DEFAULT_DATE_TIME_FORMAT.to_string()),
filter_module: vec![],
};
init_log_conf(config)?;
Ok(())
Expand Down Expand Up @@ -563,6 +573,14 @@ fn build_config(log: &LogConfig) -> SimpleResult<Config> {
}
}

for module_name in &log.filter_module {
config_builder = config_builder.logger(LoggerBuilder::build(
Logger::builder(),
module_name,
LevelFilter::Off,
));
}

let config = config_builder
.build(root_builder.build(log_level::form_log_level(&log.level)))
.map_err(|e| e.to_string())?;
Expand Down

0 comments on commit 5073ab9

Please sign in to comment.