🛈 Note: This is pre-release documentation for the upcoming tracing 0.2.0 ecosystem.

For the release documentation, please see docs.rs, instead.

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-subscriber filtering (using its Filter implementation) and global filtering (using its Subscribe implementation).

See the documentation on filtering with subscribers for details.

This is equivalent to calling FilterFn::new.

§Examples

use tracing_subscriber::{
    subscribe::{Subscribe, CollectExt},
    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_subscriber = tracing_subscriber::fmt::subscriber();

tracing_subscriber::registry()
    .with(my_subscriber.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...");