From 853929e7493ba0c5e27a274fb4895e5c6b453908 Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin Date: Mon, 29 May 2023 15:42:12 +0400 Subject: [PATCH] feat(bin, tracing): allow multiple directives for journald/file logging (#2882) --- bin/reth/src/cli.rs | 18 ++++++++---------- crates/tracing/src/lib.rs | 14 ++++++-------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/bin/reth/src/cli.rs b/bin/reth/src/cli.rs index 9b5ae13a1..f6ca0fa8c 100644 --- a/bin/reth/src/cli.rs +++ b/bin/reth/src/cli.rs @@ -10,17 +10,16 @@ use crate::{ use clap::{ArgAction, Args, Parser, Subcommand}; use reth_tracing::{ tracing::{metadata::LevelFilter, Level, Subscriber}, - tracing_subscriber::{filter::Directive, registry::LookupSpan}, + tracing_subscriber::{filter::Directive, registry::LookupSpan, EnvFilter}, BoxedLayer, FileWorkerGuard, }; -use std::str::FromStr; /// Parse CLI options, set up logging and run the chosen command. pub fn run() -> eyre::Result<()> { let opt = Cli::parse(); let mut layers = vec![reth_tracing::stdout(opt.verbosity.directive())]; - if let Some((layer, _guard)) = opt.logs.layer() { + if let Some((layer, _guard)) = opt.logs.layer()? { layers.push(layer); } reth_tracing::init(layers); @@ -115,21 +114,20 @@ pub struct Logs { impl Logs { /// Builds a tracing layer from the current log options. - pub fn layer(&self) -> Option<(BoxedLayer, Option)> + pub fn layer(&self) -> eyre::Result, Option)>> where S: Subscriber, for<'a> S: LookupSpan<'a>, { - let directive = Directive::from_str(self.filter.as_str()) - .unwrap_or_else(|_| Directive::from_str("debug").unwrap()); + let filter = EnvFilter::builder().parse(&self.filter)?; if self.journald { - Some((reth_tracing::journald(directive).expect("Could not connect to journald"), None)) + Ok(Some((reth_tracing::journald(filter).expect("Could not connect to journald"), None))) } else if self.persistent { - let (layer, guard) = reth_tracing::file(directive, &self.log_directory, "reth.log"); - Some((layer, Some(guard))) + let (layer, guard) = reth_tracing::file(filter, &self.log_directory, "reth.log"); + Ok(Some((layer, Some(guard)))) } else { - None + Ok(None) } } } diff --git a/crates/tracing/src/lib.rs b/crates/tracing/src/lib.rs index 142497ab2..ffe09ec54 100644 --- a/crates/tracing/src/lib.rs +++ b/crates/tracing/src/lib.rs @@ -58,12 +58,12 @@ where /// Builds a new tracing layer that appends to a log file. /// -/// The events are filtered by `directive`. +/// The events are filtered by `filter`. /// /// The boxed layer and a guard is returned. When the guard is dropped the buffer for the log /// file is immediately flushed to disk. Any events after the guard is dropped may be missed. pub fn file( - directive: impl Into, + filter: EnvFilter, dir: impl AsRef, file_name: impl AsRef, ) -> (BoxedLayer, tracing_appender::non_blocking::WorkerGuard) @@ -76,7 +76,7 @@ where let layer = tracing_subscriber::fmt::layer() .with_ansi(false) .with_writer(writer) - .with_filter(EnvFilter::default().add_directive(directive.into())) + .with_filter(filter) .boxed(); (layer, guard) @@ -90,17 +90,15 @@ pub type FileWorkerGuard = tracing_appender::non_blocking::WorkerGuard; /// Builds a new tracing layer that writes events to journald. /// -/// The events are filtered by `directive`. +/// The events are filtered by `filter`. /// /// If the layer cannot connect to journald for any reason this function will return an error. -pub fn journald(directive: impl Into) -> std::io::Result> +pub fn journald(filter: EnvFilter) -> std::io::Result> where S: Subscriber, for<'a> S: LookupSpan<'a>, { - Ok(tracing_journald::layer()? - .with_filter(EnvFilter::default().add_directive(directive.into())) - .boxed()) + Ok(tracing_journald::layer()?.with_filter(filter).boxed()) } /// Initializes a tracing subscriber for tests.