feat(bin): parse RUST_LOG in journald and log file filters (#5735)

This commit is contained in:
Alexey Shekhirin
2023-12-12 12:07:28 +00:00
committed by GitHub
parent 7efd0fd939
commit 829d3ede65

View File

@ -23,10 +23,10 @@ pub mod components;
pub mod config; pub mod config;
pub mod ext; pub mod ext;
/// Default [Directive] for [EnvFilter] which disables high-frequency debug logs from `hyper` and /// Default [directives](Directive) for [EnvFilter] which disables high-frequency debug logs from
/// `trust-dns` /// `hyper` and `trust-dns`
const DEFAULT_ENV_FILTER_DIRECTIVE: &str = const DEFAULT_ENV_FILTER_DIRECTIVES: [&str; 3] =
"hyper::proto::h1=off,trust_dns_proto=off,trust_dns_resolver=off"; ["hyper::proto::h1=off", "trust_dns_proto=off", "atrust_dns_resolver=off"];
/// The main reth cli interface. /// The main reth cli interface.
/// ///
@ -232,20 +232,32 @@ impl Logs {
{ {
let mut layers = Vec::new(); 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<EnvFilter> {
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 { if self.journald {
let journald_filter = create_env_filter(&self.journald_filter)?;
layers.push( layers.push(
reth_tracing::journald( reth_tracing::journald(journald_filter).expect("Could not connect to journald"),
EnvFilter::try_new(DEFAULT_ENV_FILTER_DIRECTIVE)?
.add_directive(self.journald_filter.parse()?),
)
.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_guard = if self.log_file_max_files > 0 {
let file_filter = create_env_filter(&self.log_file_filter)?;
let (layer, guard) = reth_tracing::file( let (layer, guard) = reth_tracing::file(
EnvFilter::try_new(DEFAULT_ENV_FILTER_DIRECTIVE)? file_filter,
.add_directive(self.log_file_filter.parse()?),
&self.log_file_directory, &self.log_file_directory,
"reth.log", "reth.log",
self.log_file_max_size * MB_TO_BYTES, self.log_file_max_size * MB_TO_BYTES,
@ -383,4 +395,21 @@ mod tests {
.unwrap(); .unwrap();
assert!(reth.run().is_err()); 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());
}
} }