From 829d3ede65a792fb26f2b5d513265bb4fd59425a Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin Date: Tue, 12 Dec 2023 12:07:28 +0000 Subject: [PATCH] feat(bin): parse `RUST_LOG` in journald and log file filters (#5735) --- bin/reth/src/cli/mod.rs | 51 ++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/bin/reth/src/cli/mod.rs b/bin/reth/src/cli/mod.rs index 1361b53d2..14f25a279 100644 --- a/bin/reth/src/cli/mod.rs +++ b/bin/reth/src/cli/mod.rs @@ -23,10 +23,10 @@ pub mod components; pub mod config; pub mod ext; -/// Default [Directive] for [EnvFilter] which disables high-frequency debug logs from `hyper` and -/// `trust-dns` -const DEFAULT_ENV_FILTER_DIRECTIVE: &str = - "hyper::proto::h1=off,trust_dns_proto=off,trust_dns_resolver=off"; +/// Default [directives](Directive) for [EnvFilter] which disables high-frequency debug logs from +/// `hyper` and `trust-dns` +const DEFAULT_ENV_FILTER_DIRECTIVES: [&str; 3] = + ["hyper::proto::h1=off", "trust_dns_proto=off", "atrust_dns_resolver=off"]; /// The main reth cli interface. /// @@ -232,20 +232,32 @@ impl Logs { { let mut layers = Vec::new(); + // Function to create a new EnvFilter with environment (from `RUST_LOG` env var), default + // (from `DEFAULT_DIRECTIVES`) and additional directives. + let create_env_filter = |additional_directives: &str| -> eyre::Result { + let env_filter = EnvFilter::builder().from_env_lossy(); + + DEFAULT_ENV_FILTER_DIRECTIVES + .into_iter() + .chain(additional_directives.split(',')) + .try_fold(env_filter, |env_filter, directive| { + Ok(env_filter.add_directive(directive.parse()?)) + }) + }; + + // Create and add the journald layer if enabled if self.journald { + let journald_filter = create_env_filter(&self.journald_filter)?; layers.push( - reth_tracing::journald( - EnvFilter::try_new(DEFAULT_ENV_FILTER_DIRECTIVE)? - .add_directive(self.journald_filter.parse()?), - ) - .expect("Could not connect to journald"), + reth_tracing::journald(journald_filter).expect("Could not connect to journald"), ); } + // Create and add the file logging layer if enabled let file_guard = if self.log_file_max_files > 0 { + let file_filter = create_env_filter(&self.log_file_filter)?; let (layer, guard) = reth_tracing::file( - EnvFilter::try_new(DEFAULT_ENV_FILTER_DIRECTIVE)? - .add_directive(self.log_file_filter.parse()?), + file_filter, &self.log_file_directory, "reth.log", self.log_file_max_size * MB_TO_BYTES, @@ -383,4 +395,21 @@ mod tests { .unwrap(); assert!(reth.run().is_err()); } + + #[test] + fn parse_env_filter_directives() { + let temp_dir = tempfile::tempdir().unwrap(); + + std::env::set_var("RUST_LOG", "info,evm=debug"); + let reth = Cli::<()>::try_parse_from([ + "reth", + "init", + "--datadir", + temp_dir.path().to_str().unwrap(), + "--log.file.filter", + "debug,net=trace", + ]) + .unwrap(); + assert!(reth.run().is_ok()); + } }