-
Notifications
You must be signed in to change notification settings - Fork 721
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
subscriber: add
Filter::event_enabled
(#2245)
## Motivation Like for `Subscriber` and `Layer`, allow per-layer `Filter`s to filter based on event fields. ## Solution Add `Filter::event_enabled`, plumb it through the combinator implementations, and call it from `Filtered`. The bit I'm the least confident about is the check in `Registry`'s implementation, but I *think* it matches what `event` is doing and everything seems to function correctly.
- Loading branch information
Showing
6 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use crate::support::*; | ||
use tracing::Level; | ||
use tracing_subscriber::{field::Visit, layer::Filter, prelude::*}; | ||
|
||
struct FilterEvent; | ||
|
||
impl<S> Filter<S> for FilterEvent { | ||
fn enabled( | ||
&self, | ||
_meta: &tracing::Metadata<'_>, | ||
_cx: &tracing_subscriber::layer::Context<'_, S>, | ||
) -> bool { | ||
true | ||
} | ||
|
||
fn event_enabled( | ||
&self, | ||
event: &tracing::Event<'_>, | ||
_cx: &tracing_subscriber::layer::Context<'_, S>, | ||
) -> bool { | ||
struct ShouldEnable(bool); | ||
impl Visit for ShouldEnable { | ||
fn record_bool(&mut self, field: &tracing_core::Field, value: bool) { | ||
if field.name() == "enable" { | ||
self.0 = value; | ||
} | ||
} | ||
|
||
fn record_debug( | ||
&mut self, | ||
_field: &tracing_core::Field, | ||
_value: &dyn core::fmt::Debug, | ||
) { | ||
} | ||
} | ||
let mut should_enable = ShouldEnable(false); | ||
event.record(&mut should_enable); | ||
should_enable.0 | ||
} | ||
} | ||
|
||
#[test] | ||
fn per_subscriber_event_field_filtering() { | ||
let (expect, handle) = layer::mock() | ||
.event(event::mock().at_level(Level::TRACE)) | ||
.event(event::mock().at_level(Level::INFO)) | ||
.done() | ||
.run_with_handle(); | ||
|
||
let _subscriber = tracing_subscriber::registry() | ||
.with(expect.with_filter(FilterEvent)) | ||
.set_default(); | ||
|
||
tracing::trace!(enable = true, "hello trace"); | ||
tracing::debug!("hello debug"); | ||
tracing::info!(enable = true, "hello info"); | ||
tracing::warn!(enable = false, "hello warn"); | ||
tracing::error!("hello error"); | ||
|
||
handle.assert_finished(); | ||
} |