[][src]Crate tracing_line_filter

A tracing filter for enabling individual spans and events by line number.

tracing is a framework for instrumenting Rust programs to collect scoped, structured, and async-aware diagnostics. The tracing-subscriber crate's EnvFilter type provides a mechanism for controlling what tracing spans and events are collected by matching their targets, verbosity levels, and fields. In some cases, though, it can be useful to toggle on or off individual spans or events with a higher level of granularity. Therefore, this crate provides a filtering Layer that enables individual spans and events based on their module path/file path and line numbers.

Since the implementation of this filter is rather simple, the source code of this crate is also useful as an example to tracing users who want to implement their own filtering logic.

Usage

First, add this to your Cargo.toml:

tracing-line-filter = "0.1"

Examples

Enabling events by line:

use tracing_line_filter::LineFilter;
mod some_module {
    pub fn do_stuff() {
        tracing::info!("i'm doing stuff");
        tracing::debug!("i'm also doing stuff!");
    }
}

fn main() {
    use tracing_subscriber::prelude::*;

    let mut filter = LineFilter::default();
    filter
        .enable_by_mod("my_crate::some_module", 6)
        .enable_by_mod("my_crate", 25)
        .enable_by_mod("my_crate", 27);

    tracing_subscriber::registry()
        .with(tracing_subscriber::fmt::layer().pretty())
        .with(filter)
        .init();

    tracing::info!("i'm not enabled");
    tracing::debug!("i'm enabled!");
    some_module::do_stuff();
    tracing::trace!("hi!");
}

Chaining a LineFilter with a tracing_subscriber EnvFilter:

use tracing_line_filter::LineFilter;
use tracing_subscriber::EnvFilter;

mod some_module {
    pub fn do_stuff() {
        tracing::info!("i'm doing stuff");
        tracing::debug!("i'm also doing stuff!");
        // This won't be enabled, because it's at the TRACE level, and the
        // `EnvFilter` only enables up to the DEBUG level.
        tracing::trace!("doing very verbose stuff");
    }
}

fn main() {
    use tracing_subscriber::prelude::*;

    let mut filter = LineFilter::default();
    filter
        .enable_by_mod("with_env_filter", 30)
        .enable_by_mod("with_env_filter", 33)
        // use an `EnvFilter` that enables DEBUG and lower in `some_module`,
        // and everything at the ERROR level.
        .with_env_filter(EnvFilter::new("error,with_env_filter::some_module=debug"));

    tracing_subscriber::registry()
        .with(tracing_subscriber::fmt::layer().pretty())
        .with(filter)
        .init();

    tracing::info!("i'm not enabled");
    tracing::debug!("i'm enabled!!");
    some_module::do_stuff();
    tracing::trace!("hi!");

    // This will be enabled by the `EnvFilter`.
    tracing::error!("an error!");
}

Structs

BadPath

Indicates a file path was invalid for use in a LineFilter.

LineFilter

A filter for enabling spans and events by file/module path and line number.