Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pitoniak32 opened this issue Jun 29, 2024 · 0 comments

Comments

@pitoniak32
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant