From d56ef364c40d57ee9e07ffbf556b5d3235e0d281 Mon Sep 17 00:00:00 2001 From: Gary Yu Date: Mon, 5 Nov 2018 11:57:59 +0800 Subject: [PATCH] fix: init_test_logger update for log4rs (#1921) --- util/src/logger.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/util/src/logger.rs b/util/src/logger.rs index 99847849b..56b550a02 100644 --- a/util/src/logger.rs +++ b/util/src/logger.rs @@ -187,8 +187,47 @@ pub fn init_test_logger() { let mut logger = LoggingConfig::default(); logger.log_to_file = false; logger.stdout_log_level = LogLevel::Debug; + + // Save current logging configuration let mut config_ref = LOGGING_CONFIG.lock(); *config_ref = logger; + + let level_stdout = convert_log_level(&config_ref.stdout_log_level); + let level_minimum = level_stdout; // minimum logging level for Root logger + + // Start logger + let stdout = ConsoleAppender::builder() + .encoder(Box::new(PatternEncoder::default())) + .build(); + + let mut root = Root::builder(); + + let mut appenders = vec![]; + + { + let filter = Box::new(ThresholdFilter::new(level_stdout)); + appenders.push( + Appender::builder() + .filter(filter) + .filter(Box::new(GrinFilter)) + .build("stdout", Box::new(stdout)), + ); + + root = root.appender("stdout"); + } + + let config = Config::builder() + .appenders(appenders) + .build(root.build(level_minimum)) + .unwrap(); + + let _ = log4rs::init_config(config).unwrap(); + + info!( + "log4rs is initialized, stdout level: {:?}, min. level: {:?}", + level_stdout, level_minimum + ); + *was_init_ref = true; }