tokio-rs / tracing

Application level tracing for Rust.
https://tracing.rs
MIT License
5.14k stars 671 forks source link

[tracing_subscriber::filter::Builder] Proposal for supporting multiple default directives / setting a default filter #3022

Open pitoniak32 opened 6 days ago

pitoniak32 commented 6 days ago

Feature Request

Crates

tracing_subscriber

specifically tracing_subscriber::filter::Builder

Motivation

I have a crate where I am setting up tracing. I would like to allow the user to optionally specify RUST_LOG to configure the tracing. But by default I would like to have an EnvFilter of warn,my_crate=debug.

Proposal

My initial thoughts were to add support on the builder, via a new function with_default_directives(directives: Vec<Directive>) to parse multiple directives, and then convert the default_directive field to a Vec<Directive>.

example of how it could be used:

let filter = EnvFilter::builder()
  .with_default_directives(vec![LevelFilter::WARN.into(), "my_crate=debug".parse()?)
  .from_env_lossy();

or another possibility could be:

with_default_filter(env_filter: EnvFilter)

let filter = EnvFilter::builder()
  .with_default_filter("warn,my_crate=debug".parse().unwrap())
  .from_env_lossy();

I am more than willing to make a contribution to add this if there is interest!

Alternatives

It's possible to do this without the new function. It just seems like a nice quality of life addition to the builder.

let filter = if std::env::var("RUST_LOG").is_ok() {
    EnvFilter::builder().from_env_lossy()
} else {
    "warn,my_crate=debug".parse()?
};

P.S. I love the tokio-rs::tracing ecosystem. Thank you for all the dedication to such clean tooling!