mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat(bin, tracing): allow multiple directives for journald/file logging (#2882)
This commit is contained in:
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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.
|
||||
|
||||
Reference in New Issue
Block a user