Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add env-filter-explorer example#3233

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
hds merged 2 commits intotokio-rs:masterfromjoshka:jm/env-filter-explorer
May 15, 2025

Conversation

joshka
Copy link
Contributor

Motivation

Ondiscord, Conrad suggested a website to experiment with env-filter syntax. I think a TUI in tree makes a pretty decent replacement, especially as the output can exactly match the output that a real user might see if they're configuring tracing_subscriber with the default values.

Solution

This example demonstrates how to use thetracing-subscriber crate's
EnvFilter type to filter log messages based on their metadata. The
example provides a text area where users can input an environment filter
string, and displays the log messages that would be captured by that
filter.

There are preset filters that can be selected that show off the syntax.

image

@joshkajoshka requested review fromhawkw,davidbarsky anda team ascode ownersMarch 12, 2025 20:59
Copy link
Contributor

@kaffarellkaffarell left a comment
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Wow, this is very nice! Did not yet look at the code in-depth but noticed one issue: When the envfilter is empty, no event is displayed (this is not correct, every event should be shown). Same on bogus input, e.g., when writing "abc" every event should be visible.

joshka reacted with thumbs up emoji
@joshka
Copy link
ContributorAuthor

This is the result of calling EnvFilterBuilder::parse(). There's nothing more to it than that.

@hds
Copy link
Contributor

hds commentedApr 11, 2025
edited
Loading

EDIT: I think I'm wrong about the implementation of this (but right about the docs), that should probably be fixed.

@kaffarell It depends on the default directive, if none is given thenERROR level is set (seehttps://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#method.new).

(This is different from if theenv-filter feature isn't enabled at all andfmt::init() is called, in which case all traces are shown)

Copy link
Contributor

@hdshds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This looks awesome! If we could avoid reformatting unrelated lines in Cargo.toml that would be good though.

joshka reacted with thumbs up emoji
tracing-futures = { version = "0.3", path = "../tracing-futures", features = ["futures-01"] }
tracing-attributes = { path = "../tracing-attributes", version = "0.2"}
tracing-log = { path = "../tracing-log", version = "0.2", features = ["env_logger"] }
tracing-subscriber = { path = "../tracing-subscriber", version = "0.3", features = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

It would be good if we could avoid reformatting. I think this isn't "standard" TOML formatting, it comes from a specific extension.

joshka reacted with thumbs up emoji
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yeah I usehttps://marketplace.visualstudio.com/items?itemName=tamasfe.even-better-toml in most projects and often forget to turn it off when viewing other projects. Apologies.

I removed the wrapping changes, but left the spacing changes as they are consistent with the rest of the file. I hope that's ok. If not I can remove those too.

@hds
Copy link
Contributor

hds commentedApr 11, 2025

OK, I wrote a test for the case as per the documentation, and it fails:

#[test]fnempty_string(){let filter:EnvFilter ="".parse().expect("filter should parse");let(collector, finished) = collector::mock().event(expect::event().at_level(Level::ERROR)).only().run_with_handle();let subscriber = collector.with(filter);with_default(subscriber, ||{        tracing::trace!("this should be disabled");        tracing::debug!("this should be disabled");        tracing::info!("this should be disabled");        tracing::warn!("this should be disabled");        tracing::error!("this shouldn't be disabled");});    finished.assert_finished();}

But it fails because nothing is received by the collector, it's interpreting an empty string asOFF.

@joshka
Copy link
ContributorAuthor

Intuitively (to me at least), empty should effectively be a noop, which for a filter probably means just default. That said, I have a vague recollection there's a bit of weirdness where if the envfilter feature is not enabled you get info level logs by default, but it changes to error level if enabled. That also seems a bit counterintuitive. (I may be misremembering and I'm not near my computer for a few days to check)

@kaffarell
Copy link
Contributor

kaffarell commentedApr 11, 2025
edited
Loading

This is the result of calling EnvFilterBuilder::parse(). There's nothing more to it than that.

Oh you're right, my bad, I tested withEnvFilter::new().

This behavior is kind weird though. We set a default_directive (with min level ERROR) on thenew() method, but nowhere else... I think we should remove the default_directive from thenew() method – that will make everything more predictable and easy to use. That will be a breaking change though.

This example demonstrates how to use the `tracing-subscriber` crate's`EnvFilter` type to filter log messages based on their metadata. Theexample provides a text area where users can input an environment filterstring, and displays the log messages that would be captured by thatfilter.
@joshkajoshkaforce-pushed thejm/env-filter-explorer branch from6c229d9 to532a222CompareMay 14, 2025 18:23
@joshka
Copy link
ContributorAuthor

joshka commentedMay 14, 2025
edited
Loading

Rebased, removed the inferno dep bump, fixed the manifest formatting.
I'm going to make the flow a bit clearer though before it's ready.

@joshkajoshka marked this pull request as draftMay 14, 2025 20:44
@joshkajoshka marked this pull request as ready for reviewMay 14, 2025 22:13
Copy link
Contributor

@hdshds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This looks great. Thank you!

@hdshds merged commit906f00c intotokio-rs:masterMay 15, 2025
56 checks passed
@joshkajoshka deleted the jm/env-filter-explorer branchMay 15, 2025 17:33
@hdshds mentioned this pull requestMay 21, 2025
25 tasks
hds pushed a commit that referenced this pull requestMay 28, 2025
This example demonstrates how to use the `tracing-subscriber` crate's`EnvFilter` type to filter log messages based on their metadata. Theexample provides a text area where users can input an environment filterstring, and displays the log messages that would be captured by thatfilter.
hds pushed a commit that referenced this pull requestJun 3, 2025
This example demonstrates how to use the `tracing-subscriber` crate's`EnvFilter` type to filter log messages based on their metadata. Theexample provides a text area where users can input an environment filterstring, and displays the log messages that would be captured by thatfilter.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@kaffarellkaffarellkaffarell left review comments

@hdshdshds approved these changes

@hawkwhawkwAwaiting requested review from hawkwhawkw is a code owner

@davidbarskydavidbarskyAwaiting requested review from davidbarskydavidbarsky is a code owner

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@joshka@hds@kaffarell

[8]ページ先頭

©2009-2025 Movatter.jp