fix(tracing): prioritize RUST_LOG and verbosity CLI arg (#6413)

This commit is contained in:
Alexey Shekhirin
2024-02-05 19:42:32 +00:00
committed by GitHub
parent f8a71047d4
commit 6c1263e000
45 changed files with 71 additions and 70 deletions

View File

@ -71,11 +71,11 @@ impl Layers {
pub(crate) fn stdout(
&mut self,
format: LogFormat,
directive: Directive,
filter: &str,
default_directive: Directive,
filters: &str,
color: Option<String>,
) -> eyre::Result<()> {
let filter = build_env_filter(Some(directive), filter)?;
let filter = build_env_filter(Some(default_directive), filters)?;
let layer = format.apply(filter, color, None);
self.inner.push(layer.boxed());
Ok(())
@ -173,7 +173,7 @@ fn build_env_filter(
DEFAULT_ENV_FILTER_DIRECTIVES
.into_iter()
.chain(directives.split(','))
.chain(directives.split(',').filter(|d| !d.is_empty()))
.try_fold(env_filter, |env_filter, directive| {
Ok(env_filter.add_directive(directive.parse()?))
})

View File

@ -19,8 +19,8 @@
//! fn main() -> eyre::Result<()> {
//! let tracer = RethTracer::new().with_stdout(LayerInfo::new(
//! LogFormat::Json,
//! LevelFilter::INFO.to_string(),
//! "debug".to_string(),
//! LevelFilter::INFO.into(),
//! None,
//! ));
//!
@ -42,8 +42,6 @@
)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
use tracing_subscriber::filter::Directive;
// Re-export tracing crates
pub use tracing;
pub use tracing_subscriber;
@ -125,8 +123,8 @@ impl Default for RethTracer {
#[derive(Debug, Clone)]
pub struct LayerInfo {
format: LogFormat,
default_directive: String,
filters: String,
directive: Directive,
color: Option<String>,
}
@ -138,16 +136,16 @@ impl LayerInfo {
/// - `LogFormat::Json` for JSON formatting.
/// - `LogFormat::LogFmt` for logfmt (key=value) formatting.
/// - `LogFormat::Terminal` for human-readable, terminal-friendly formatting.
/// * `default_directive` - Directive for filtering log messages.
/// * `filters` - Additional filtering parameters as a string.
/// * `directive` - Directive for filtering log messages.
/// * `color` - Optional color configuration for the log messages.
pub fn new(
format: LogFormat,
default_directive: String,
filters: String,
directive: Directive,
color: Option<String>,
) -> Self {
Self { format, directive, filters, color }
Self { format, default_directive, filters, color }
}
}
@ -159,8 +157,8 @@ impl Default for LayerInfo {
fn default() -> Self {
Self {
format: LogFormat::Terminal,
directive: LevelFilter::INFO.into(),
filters: "debug".to_string(),
default_directive: LevelFilter::INFO.to_string(),
filters: "".to_string(),
color: Some("always".to_string()),
}
}
@ -196,7 +194,7 @@ impl Tracer for RethTracer {
layers.stdout(
self.stdout.format,
self.stdout.directive,
self.stdout.default_directive.parse()?,
&self.stdout.filters,
self.stdout.color,
)?;