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

subscriber: add Filtered::filter_mut #1959

Merged
merged 1 commit into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions tracing-subscriber/src/filter/layer_filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,40 @@ impl<L, F, S> Filtered<L, F, S> {
fn did_enable(&self, f: impl FnOnce()) {
FILTERING.with(|filtering| filtering.did_enable(self.id(), f))
}

tfreiberg-fastly marked this conversation as resolved.
Show resolved Hide resolved
/// Borrows the [`Filter`](crate::layer::Filter) used by this layer.
pub fn filter(&self) -> &F {
&self.filter
}

/// Mutably borrows the [`Filter`](crate::layer::Filter) used by this layer.
tfreiberg-fastly marked this conversation as resolved.
Show resolved Hide resolved
///
/// When this layer can be mutably borrowed, this may be used to mutate the filter.
/// Generally, this will primarily be used with the
/// [`reload::Handle::modify`](crate::reload::Handle::modify) method.
///
/// # Examples
///
/// ```
/// # use tracing::info;
/// # use tracing_subscriber::{filter,fmt,reload,Registry,prelude::*};
/// # fn main() {
/// let filtered_layer = fmt::Layer::default().with_filter(filter::LevelFilter::WARN);
/// let (filtered_layer, reload_handle) = reload::Layer::new(filtered_layer);
/// #
/// # // specifying the Registry type is required
/// # let _: &reload::Handle<filter::Filtered<fmt::Layer<Registry>,
/// # filter::LevelFilter, Registry>,Registry>
/// # = &reload_handle;
/// #
/// info!("This will be ignored");
/// reload_handle.modify(|layer| *layer.filter_mut() = filter::LevelFilter::INFO);
/// info!("This will be logged");
/// # }
/// ```
pub fn filter_mut(&mut self) -> &mut F {
&mut self.filter
}
}

impl<S, L, F> Layer<S> for Filtered<L, F, S>
Expand Down
29 changes: 28 additions & 1 deletion tracing-subscriber/src/reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,28 @@
//! change at runtime. Note that this layer introduces a (relatively small)
//! amount of overhead, and should thus only be used as needed.
//!
//! # Examples
//!
//! Reloading a [`Filtered`](crate::filter::Filtered) layer to change the filter at runtime.
//!
//! ```
//! # use tracing::info;
//! # use tracing_subscriber::{filter,fmt,reload,Registry,prelude::*};
//! # fn main() {
//! let filtered_layer = fmt::Layer::default().with_filter(filter::LevelFilter::WARN);
//! let (filtered_layer, reload_handle) = reload::Layer::new(filtered_layer);
//! #
//! # // specifying the Registry type is required
//! # let _: &reload::Handle<filter::Filtered<fmt::Layer<Registry>,
//! # filter::LevelFilter, Registry>,Registry>
//! # = &reload_handle;
//! #
//! info!("This will be ignored");
//! reload_handle.modify(|layer| *layer.filter_mut() = filter::LevelFilter::INFO);
//! info!("This will be logged");
//! # }
//! ```
//!
//! [`Layer` type]: struct.Layer.html
//! [`Layer` trait]: ../layer/trait.Layer.html
use crate::layer;
Expand Down Expand Up @@ -36,7 +58,7 @@ pub struct Layer<L, S> {
_s: PhantomData<fn(S)>,
}

/// Allows reloading the state of an associated `Layer`.
/// Allows reloading the state of an associated [`Layer`](crate::layer::Layer).
#[derive(Debug)]
pub struct Handle<L, S> {
inner: Weak<RwLock<L>>,
Expand Down Expand Up @@ -150,6 +172,11 @@ where
S: Subscriber,
{
/// Replace the current layer with the provided `new_layer`.
///
/// **Warning:** The [`Filtered`](crate::filter::Filtered) type currently can't be changed
/// at runtime via the [`Handle::reload`] method.
/// Use the [`Handle::modify`] method to change the filter instead.
/// (see <https://github.com/tokio-rs/tracing/issues/1629>)
pub fn reload(&self, new_layer: impl Into<L>) -> Result<(), Error> {
self.modify(|layer| {
*layer = new_layer.into();
Expand Down