Skip to content

Commit 501bcf6

Browse files
tfreiberg-fastlykaffarell
authored andcommitted
subscriber: add Filtered::filter_mut method (tokio-rs#1959)
Partially fixes tokio-rs#1629 (I think making `reload::Handle::reload` work with `Filtered` would be cleaner, but this approach seemed easier to me) I assumed opening the PR against v0.1.x is correct as I couldn't find the `Filtered` type in master. I think it'd be sensible to note the fact that `reload::Handle::reload` doesn't work with `Filtered` in the docs somewhere, should I add that? ## Motivation Changing the filter of a `Filtered` at runtime is currently only possible by replacing it with a new `Filtered` via the `reload::Handle::reload` method. This currently doesn't work as the new `Filtered` won't receive a `FilterId` (see tokio-rs#1629). While it would be desirable to just make that work, it would only be possible via a breaking change (according to Eliza) so this seems like a reasonable (and easy) workaround for now. (I can't judge whether this method is only useful as a workaround for the bug or if it suits the public API independently) ## Solution Offer mutable access to the `Filtered::filter` field in the public API. This can be used via the `reload::Handle::modify` method to change the filter inside the existing `Filtered`. Fixes tokio-rs#1629
1 parent 5427615 commit 501bcf6

File tree

1 file changed

+34
-0
lines changed
  • tracing-subscriber/src/filter/layer_filters

1 file changed

+34
-0
lines changed

tracing-subscriber/src/filter/layer_filters/mod.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,40 @@ impl<L, F, S> Filtered<L, F, S> {
470470
fn did_enable(&self, f: impl FnOnce()) {
471471
FILTERING.with(|filtering| filtering.did_enable(self.id(), f))
472472
}
473+
474+
/// Borrows the [`Filter`](crate::layer::Filter) used by this layer.
475+
pub fn filter(&self) -> &F {
476+
&self.filter
477+
}
478+
479+
/// Mutably borrows the [`Filter`](crate::layer::Filter) used by this layer.
480+
///
481+
/// When this layer can be mutably borrowed, this may be used to mutate the filter.
482+
/// Generally, this will primarily be used with the
483+
/// [`reload::Handle::modify`](crate::reload::Handle::modify) method.
484+
///
485+
/// # Examples
486+
///
487+
/// ```
488+
/// # use tracing::info;
489+
/// # use tracing_subscriber::{filter,fmt,reload,Registry,prelude::*};
490+
/// # fn main() {
491+
/// let filtered_layer = fmt::Layer::default().with_filter(filter::LevelFilter::WARN);
492+
/// let (filtered_layer, reload_handle) = reload::Layer::new(filtered_layer);
493+
/// #
494+
/// # // specifying the Registry type is required
495+
/// # let _: &reload::Handle<filter::Filtered<fmt::Layer<Registry>,
496+
/// # filter::LevelFilter, Registry>,Registry>
497+
/// # = &reload_handle;
498+
/// #
499+
/// info!("This will be ignored");
500+
/// reload_handle.modify(|layer| *layer.filter_mut() = filter::LevelFilter::INFO);
501+
/// info!("This will be logged");
502+
/// # }
503+
/// ```
504+
pub fn filter_mut(&mut self) -> &mut F {
505+
&mut self.filter
506+
}
473507
}
474508

475509
impl<S, L, F> Layer<S> for Filtered<L, F, S>

0 commit comments

Comments
 (0)