pub fn filter_fn<F>(f: F) -> FilterFn<F> 
where F: Fn(&Metadata<'_>) -> bool,
Expand description

Constructs a FilterFn, from a function or closure that returns true if a span or event should be enabled, based on its Metadata.

The returned FilterFn can be used for both per-layer filtering (using its Filter implementation) and global filtering (using its Layer implementation).

See the documentation on filtering with layers for details.

This is equivalent to calling FilterFn::new.

Examples

use tracing_subscriber::{
    layer::{Layer, SubscriberExt},
    filter,
    util::SubscriberInitExt,
};

let my_filter = filter::filter_fn(|metadata| {
    // Only enable spans or events with the target "interesting_things"
    metadata.target() == "interesting_things"
});

let my_layer = tracing_subscriber::fmt::layer();

tracing_subscriber::registry()
    .with(my_layer.with_filter(my_filter))
    .init();

// This event will not be enabled.
tracing::warn!("something important but uninteresting happened!");

// This event will be enabled.
tracing::debug!(target: "interesting_things", "an interesting minor detail...");