feat(logger.rs): honor RUST_LOG for per-module log filtering

Use the tracing-subscriber `EnvFilter::builder()` +
`from_env_lossy()` pattern so `RUST_LOG`, when set, takes
precedence over the `--log-level` flag. This lets operators
silence noisy dependencies (e.g. radicle-cob's "Loading object
of type ..." debug stream emitted with `target: "cob"`) while
keeping the broker itself at `debug` level.

Example: `RUST_LOG="debug,cob=info" cib ...`

`from_env_lossy()` also reports invalid directives to stderr
("ignoring '<directive>': <err>") instead of swallowing them,
so this allows operators to quickly realise if they've set the
wrong log level.

This also works with `log` crate records (most heartwood crates),
as they are bridged into tracing via tracing-subscriber's
default-enabled `tracing-log` feature, so they respect the
same directives.

Also fixes typos in the `--log-level` doc-comment.

Signed-off-by: Yorgos Saslis <yorgos.work@proton.me>
This commit is contained in:
Yorgos Saslis
2026-05-19 17:37:49 +03:00
parent 3273b20686
commit 33d5a514a2
2 changed files with 9 additions and 4 deletions
+6 -2
View File
@@ -61,8 +61,12 @@ struct Args {
#[clap(long)]
config: PathBuf,
/// Se the global log level. Log messages that on at least this
/// level get written to the standard error output.
/// Set the global log level. Log messages at this level or
/// higher get written to the standard error output.
///
/// Overridden by the `RUST_LOG` environment variable when set,
/// which accepts per-module directives like `debug,cob=info`
/// for finer control over noisy dependencies.
#[clap(long, value_enum)]
log_level: Option<logger::LogLevel>,
+3 -2
View File
@@ -271,7 +271,6 @@ impl From<LogLevel> for tracing::Level {
}
}
#[allow(clippy::unwrap_used)]
pub fn open(level: LogLevel) {
#[cfg(test)]
let writer = fmt::TestWriter::new();
@@ -281,7 +280,9 @@ pub fn open(level: LogLevel) {
let fmt_layer = fmt::layer().with_target(false).with_writer(writer).json();
let filter_layer = EnvFilter::try_new(level.to_string()).unwrap();
let filter_layer = EnvFilter::builder()
.with_default_directive(Level::from(level).into())
.from_env_lossy();
tracing_subscriber::registry()
.with(filter_layer)