Skip to content

Commit

Permalink
Configure logging from env as well
Browse files Browse the repository at this point in the history
Currently, it is not possible to have module-level granularity configuration for logging. Setting up `-vvv` makes IPA to log all trace events, including those coming from third-party.

This change enables this functionality by optionally reading logging configuration from `RUST_LOG`
  • Loading branch information
akoshelev committed Nov 1, 2023
1 parent 6ea4069 commit da2ee44
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/cli/verbosity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use clap::Parser;
use metrics_tracing_context::MetricsLayer;
use tracing::{info, metadata::LevelFilter, Level};
use tracing_subscriber::{
fmt, fmt::format::FmtSpan, layer::SubscriberExt, util::SubscriberInitExt,
fmt, fmt::format::FmtSpan, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter,
};

use crate::{
Expand All @@ -31,14 +31,14 @@ pub struct LoggingHandle {
impl Verbosity {
#[must_use]
pub fn setup_logging(&self) -> LoggingHandle {
let filter_layer = self.level_filter();
let filter_layer = self.log_filter();
let fmt_layer = fmt::layer()
.with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
.with_ansi(std::io::stderr().is_terminal())
.with_writer(stderr);

tracing_subscriber::registry()
.with(self.level_filter())
.with(self.log_filter())
.with(fmt_layer)
.with(MetricsLayer::new())
.init();
Expand All @@ -53,15 +53,20 @@ impl Verbosity {
handle
}

fn level_filter(&self) -> LevelFilter {
if self.quiet {
LevelFilter::OFF
} else {
LevelFilter::from_level(match self.verbose {
0 => Level::INFO,
1 => Level::DEBUG,
_ => Level::TRACE,
})
}
fn log_filter(&self) -> EnvFilter {
EnvFilter::builder()
.with_default_directive(
if self.quiet {
LevelFilter::OFF
} else {
LevelFilter::from_level(match self.verbose {
0 => Level::INFO,
1 => Level::DEBUG,
_ => Level::TRACE,
})
}
.into(),
)
.from_env_lossy()
}
}

0 comments on commit da2ee44

Please sign in to comment.