Set the log level of log correctly. (#296)
Some checks are pending
abi-consistent-check / build-and-compare (push) Waiting to run
code-coverage / unittest-cov (push) Waiting to run
rust / check (push) Waiting to run
rust / test (push) Waiting to run
rust / lints (push) Waiting to run
functional-test / test (push) Waiting to run

* Set the log level of `log` correctly.

* Update the log level dynamically.
This commit is contained in:
peilun-conflux 2024-12-10 05:37:25 +08:00 committed by GitHub
parent d3a2118985
commit 5b8b8971ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 13 deletions

7
Cargo.lock generated
View File

@ -4596,9 +4596,9 @@ dependencies = [
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.21" version = "0.4.22"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
dependencies = [ dependencies = [
"serde", "serde",
"value-bag", "value-bag",
@ -8931,6 +8931,7 @@ dependencies = [
"futures", "futures",
"itertools 0.10.5", "itertools 0.10.5",
"libp2p", "libp2p",
"log",
"log_entry_sync", "log_entry_sync",
"metrics", "metrics",
"miner", "miner",
@ -8950,6 +8951,8 @@ dependencies = [
"toml 0.5.11", "toml 0.5.11",
"tracing", "tracing",
"tracing-appender", "tracing-appender",
"tracing-core",
"tracing-log",
"tracing-subscriber", "tracing-subscriber",
"zgs_spec", "zgs_spec",
"zgs_version", "zgs_version",

View File

@ -39,6 +39,9 @@ config = "0.14"
public-ip = "0.2" public-ip = "0.2"
ethers = "2.0.14" ethers = "2.0.14"
metrics = { workspace = true } metrics = { workspace = true }
rust-log = { package = "log", version = "0.4.22" }
tracing-core = "0.1.32"
tracing-log = "0.2.0"
[dependencies.libp2p] [dependencies.libp2p]
version = "0.45.1" version = "0.45.1"

View File

@ -1,5 +1,6 @@
use task_executor::TaskExecutor; use task_executor::TaskExecutor;
use tracing::Level; use tracing::Level;
use tracing_log::AsLog;
use tracing_subscriber::EnvFilter; use tracing_subscriber::EnvFilter;
const LOG_RELOAD_PERIOD_SEC: u64 = 30; 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) { pub fn configure(log_level_file: &str, log_directory: &str, executor: TaskExecutor) {
let file_appender = tracing_appender::rolling::daily(log_directory, "zgs.log"); let file_appender = tracing_appender::rolling::daily(log_directory, "zgs.log");
let (non_blocking, guard) = tracing_appender::non_blocking(file_appender); 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() let builder = tracing_subscriber::fmt()
.with_max_level(Level::TRACE) .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_writer(non_blocking)
.with_ansi(false) .with_ansi(false)
// .with_file(true) // .with_file(true)
@ -20,15 +29,6 @@ pub fn configure(log_level_file: &str, log_directory: &str, executor: TaskExecut
let handle = builder.reload_handle(); let handle = builder.reload_handle();
builder.init(); 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 // periodically check for config changes
executor.spawn( executor.spawn(
async move { 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); println!("Updating log config to {:?}", new_config);
match handle.reload(&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) => { Err(e) => {
println!("Failed to load new config: {:?}", e); println!("Failed to load new config: {:?}", e);
} }