feat(bin, tracing): allow multiple directives for journald/file logging (#2882)

This commit is contained in:
Alexey Shekhirin
2023-05-29 15:42:12 +04:00
committed by GitHub
parent 5b73b2a1b1
commit 853929e749
2 changed files with 14 additions and 18 deletions

View File

@ -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<S>(&self) -> Option<(BoxedLayer<S>, Option<FileWorkerGuard>)>
pub fn layer<S>(&self) -> eyre::Result<Option<(BoxedLayer<S>, Option<FileWorkerGuard>)>>
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)
}
}
}

View File

@ -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<S>(
directive: impl Into<Directive>,
filter: EnvFilter,
dir: impl AsRef<Path>,
file_name: impl AsRef<Path>,
) -> (BoxedLayer<S>, 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<S>(directive: impl Into<Directive>) -> std::io::Result<BoxedLayer<S>>
pub fn journald<S>(filter: EnvFilter) -> std::io::Result<BoxedLayer<S>>
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.