From 5b8b8971caea9766a76a02e57c2d18eb21b8bf23 Mon Sep 17 00:00:00 2001 From: peilun-conflux <48905552+peilun-conflux@users.noreply.github.com> Date: Tue, 10 Dec 2024 05:37:25 +0800 Subject: [PATCH] Set the log level of `log` correctly. (#296) * Set the log level of `log` correctly. * Update the log level dynamically. --- Cargo.lock | 7 +++++-- node/Cargo.toml | 3 +++ node/src/log.rs | 25 ++++++++++++++----------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d1d0c00..a8cf6b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4596,9 +4596,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.21" +version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" dependencies = [ "serde", "value-bag", @@ -8931,6 +8931,7 @@ dependencies = [ "futures", "itertools 0.10.5", "libp2p", + "log", "log_entry_sync", "metrics", "miner", @@ -8950,6 +8951,8 @@ dependencies = [ "toml 0.5.11", "tracing", "tracing-appender", + "tracing-core", + "tracing-log", "tracing-subscriber", "zgs_spec", "zgs_version", diff --git a/node/Cargo.toml b/node/Cargo.toml index e9d5211..6441cb1 100644 --- a/node/Cargo.toml +++ b/node/Cargo.toml @@ -39,6 +39,9 @@ config = "0.14" public-ip = "0.2" ethers = "2.0.14" metrics = { workspace = true } +rust-log = { package = "log", version = "0.4.22" } +tracing-core = "0.1.32" +tracing-log = "0.2.0" [dependencies.libp2p] version = "0.45.1" diff --git a/node/src/log.rs b/node/src/log.rs index db8c1ce..3060b02 100644 --- a/node/src/log.rs +++ b/node/src/log.rs @@ -1,5 +1,6 @@ use task_executor::TaskExecutor; use tracing::Level; +use tracing_log::AsLog; use tracing_subscriber::EnvFilter; const LOG_RELOAD_PERIOD_SEC: u64 = 30; @@ -7,9 +8,17 @@ const LOG_RELOAD_PERIOD_SEC: u64 = 30; pub fn configure(log_level_file: &str, log_directory: &str, executor: TaskExecutor) { let file_appender = tracing_appender::rolling::daily(log_directory, "zgs.log"); let (non_blocking, guard) = tracing_appender::non_blocking(file_appender); + + let level_file = log_level_file.trim_end().to_string(); + // load config synchronously + let mut config = std::fs::read_to_string(&level_file) + .unwrap_or_default() + .trim_end() + .to_string(); + let builder = tracing_subscriber::fmt() .with_max_level(Level::TRACE) - .with_env_filter(EnvFilter::default()) + .with_env_filter(EnvFilter::try_new(config.clone()).expect("invalid log level")) .with_writer(non_blocking) .with_ansi(false) // .with_file(true) @@ -20,15 +29,6 @@ pub fn configure(log_level_file: &str, log_directory: &str, executor: TaskExecut let handle = builder.reload_handle(); builder.init(); - let level_file = log_level_file.trim_end().to_string(); - - // load config synchronously - let mut config = std::fs::read_to_string(&level_file) - .unwrap_or_default() - .trim_end() - .to_string(); - let _ = handle.reload(&config); - // periodically check for config changes executor.spawn( async move { @@ -58,7 +58,10 @@ pub fn configure(log_level_file: &str, log_directory: &str, executor: TaskExecut println!("Updating log config to {:?}", new_config); match handle.reload(&new_config) { - Ok(()) => config = new_config, + Ok(()) => { + rust_log::set_max_level(tracing_core::LevelFilter::current().as_log()); + config = new_config + } Err(e) => { println!("Failed to load new config: {:?}", e); }