mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
refactor: move cli trace helpers to reth-tracing (#820)
This commit is contained in:
@ -9,4 +9,7 @@ description = "tracing helpers"
|
||||
|
||||
[dependencies]
|
||||
tracing = { version = "0.1", default-features = false }
|
||||
tracing-subscriber = { version = "0.3", default-features = false, features = ["env-filter", "fmt"] }
|
||||
tracing-subscriber = { version = "0.3", default-features = false, features = ["env-filter", "fmt"] }
|
||||
tracing-futures = "0.2"
|
||||
tracing-appender = "0.2"
|
||||
tracing-journald = "0.3"
|
||||
|
||||
@ -4,19 +4,115 @@
|
||||
no_crate_inject,
|
||||
attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables))
|
||||
))]
|
||||
//! reth tracing subscribers and utilities.
|
||||
//!
|
||||
//! Contains a standardized set of layers:
|
||||
//!
|
||||
//! - [`stdout()`]
|
||||
//! - [`file()`]
|
||||
//! - [`journald()`]
|
||||
//!
|
||||
//! As well as a simple way to initialize a subscriber: [`init`].
|
||||
use std::path::Path;
|
||||
use tracing::Subscriber;
|
||||
use tracing_subscriber::{
|
||||
filter::Directive, prelude::*, registry::LookupSpan, EnvFilter, Layer, Registry,
|
||||
};
|
||||
|
||||
//! reth-tracing
|
||||
|
||||
// re-export tracing crates.
|
||||
// Re-export tracing crates
|
||||
pub use tracing;
|
||||
pub use tracing_subscriber;
|
||||
|
||||
/// Initialises a tracing subscriber via `RUST_LOG` environment variable filter.
|
||||
/// A boxed tracing [Layer].
|
||||
pub type BoxedLayer<S> = Box<dyn Layer<S> + Send + Sync>;
|
||||
|
||||
/// Initializes a new [Subscriber] based on the given layers.
|
||||
pub fn init(layers: Vec<BoxedLayer<Registry>>) {
|
||||
tracing_subscriber::registry().with(layers).init();
|
||||
}
|
||||
|
||||
/// Builds a new tracing layer that writes to stdout.
|
||||
///
|
||||
/// Note: This ignores any error and should be used for testing.
|
||||
pub fn init_tracing() {
|
||||
/// The events are filtered by `default_directive`, unless overriden by `RUST_LOG`.
|
||||
///
|
||||
/// Colors can be disabled with `RUST_LOG_STYLE=never`, and event targets can be displayed with
|
||||
/// `RUST_LOG_TARGET=1`.
|
||||
pub fn stdout<S>(default_directive: impl Into<Directive>) -> BoxedLayer<S>
|
||||
where
|
||||
S: Subscriber,
|
||||
for<'a> S: LookupSpan<'a>,
|
||||
{
|
||||
// TODO: Auto-detect
|
||||
let with_ansi = std::env::var("RUST_LOG_STYLE").map(|val| val != "never").unwrap_or(true);
|
||||
let with_target = std::env::var("RUST_LOG_TARGET").map(|val| val != "0").unwrap_or(false);
|
||||
|
||||
let filter =
|
||||
EnvFilter::builder().with_default_directive(default_directive.into()).from_env_lossy();
|
||||
|
||||
tracing_subscriber::fmt::layer()
|
||||
.with_ansi(with_ansi)
|
||||
.with_target(with_target)
|
||||
.with_filter(filter)
|
||||
.boxed()
|
||||
}
|
||||
|
||||
/// Builds a new tracing layer that appends to a log file.
|
||||
///
|
||||
/// The events are filtered by `directive`.
|
||||
///
|
||||
/// 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>,
|
||||
dir: impl AsRef<Path>,
|
||||
file_name: impl AsRef<Path>,
|
||||
) -> (BoxedLayer<S>, tracing_appender::non_blocking::WorkerGuard)
|
||||
where
|
||||
S: Subscriber,
|
||||
for<'a> S: LookupSpan<'a>,
|
||||
{
|
||||
let (writer, guard) =
|
||||
tracing_appender::non_blocking(tracing_appender::rolling::never(dir, file_name));
|
||||
let layer = tracing_subscriber::fmt::layer()
|
||||
.with_ansi(false)
|
||||
.with_writer(writer)
|
||||
.with_filter(EnvFilter::default().add_directive(directive.into()))
|
||||
.boxed();
|
||||
|
||||
(layer, guard)
|
||||
}
|
||||
|
||||
/// A worker guard returned by [`file()`].
|
||||
///
|
||||
/// When a guard is dropped, all events currently in-memory are flushed to the log file this guard
|
||||
/// belongs to.
|
||||
pub type FileWorkerGuard = tracing_appender::non_blocking::WorkerGuard;
|
||||
|
||||
/// Builds a new tracing layer that writes events to journald.
|
||||
///
|
||||
/// The events are filtered by `directive`.
|
||||
///
|
||||
/// 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>>
|
||||
where
|
||||
S: Subscriber,
|
||||
for<'a> S: LookupSpan<'a>,
|
||||
{
|
||||
Ok(tracing_journald::layer()?
|
||||
.with_filter(EnvFilter::default().add_directive(directive.into()))
|
||||
.boxed())
|
||||
}
|
||||
|
||||
/// Initializes a tracing subscriber for tests.
|
||||
///
|
||||
/// The filter is configurable via `RUST_LOG`.
|
||||
///
|
||||
/// # Note
|
||||
///
|
||||
/// The subscriber will silently fail if it could not be installed.
|
||||
pub fn init_test_tracing() {
|
||||
let _ = tracing_subscriber::fmt()
|
||||
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
|
||||
.with_env_filter(EnvFilter::from_default_env())
|
||||
.with_writer(std::io::stderr)
|
||||
.try_init();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user