[][src]Struct tracing_line_filter::LineFilter

pub struct LineFilter { /* fields omitted */ }

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

Implementations

impl LineFilter[src]

pub fn new() -> Self[src]

Returns a new LineFilter.

By default, no spans and events are enabled.

pub fn with_env_filter(&mut self, env: EnvFilter) -> &mut Self[src]

Composes self with an EnvFilter that will be checked for spans and events if they are not in the lists of enabled (module, line) and (file, line) pairs.

Examples

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

let mut filter = LineFilter::default();
filter
    .enable_by_mod("my_crate", 28)
    .enable_by_mod("my_crate::my_module", 16)
    // use an ``EnvFilter` that enables DEBUG and lower in `some_module`, and
    // all ERROR spans or events, regardless of location.
    .with_env_filter(EnvFilter::new("error,my_crate::some_other_module=debug"));

pub fn enable_by_mod(
    &mut self,
    module: impl Into<Cow<'static, str>>,
    line: u32
) -> &mut Self
[src]

Enable a span or event in the Rust module module on line line.

Notes

  • Module paths should include the name of the crate. For example, the module my_module in my_crate would have path my_crate::my_module.
  • If no span or event exists at the specified location, or if the module path does not exist, this will silently do nothing.
  • Line numbers are relative to the start of the file, not to the start of the module. If a module does not have its own file (i.e., it's defined like mod my_module { ... }), the line number is relative to the containing file.

Examples

Enabling an event:

use tracing_line_filter::LineFilter;

mod my_module {
    pub fn do_stuff() {
        tracing::info!("doing stuff!")
        // ...
    }
}

// Build a line filter to enable the event in `do_stuff`.
let mut filter = LineFilter::default();
filter.enable_by_mod("my_crate::my_module", 5);

// Build a subscriber and enable that filter.
use tracing_subscriber::prelude::*;

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

// Now, the event is enabled!
my_module::do_stuff();

The std::module_path! macro can be used to enable an event in the current module:

use tracing_line_filter::LineFilter;

pub fn do_stuff() {
    tracing::info!("doing stuff!")
    // ...
}

let mut filter = LineFilter::default();
filter.enable_by_mod(module_path!(), 4);

 // ...

pub fn enable_by_file(
    &mut self,
    file: impl AsRef<Path>,
    line: u32
) -> Result<&mut Self, BadPath>
[src]

Enable a span or event in the file file on line line.

Notes

These file paths must match the file paths emitted by the std::file! macro. In particular:

  • Paths must be absolute.
  • Paths must be Rust source code files.
  • Paths must be valid UTF-8.

This method validates paths and returns an error if the path is not valid for use in a LineFilter.

Since these paths are absolute, files in Cargo dependencies will include their full path in the local Cargo registry. For example:

/home/eliza/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.0.0/src/util/trace.rs

Therefore, it can be challenging for humans to determine the correct path to a file, especially when it is in a dependency. For this reason, it's likely best to prefer Rust module paths rather than file paths when accepting input from users directly. Enabling events and spans by file paths is primarily intended for use by automated tools.

pub fn with_modules<I>(
    &mut self,
    modules: impl IntoIterator<Item = (I, u32)>
) -> &mut Self where
    I: Into<Cow<'static, str>>, 
[src]

Enable a set of spans or events by module path.

This is equivalent to repeatedly calling [enable_by_mod].

Examples

use tracing_line_filter::LineFilter;

mod foo {
    pub fn do_stuff() {
        tracing::info!("doing stuff!")
        // ...
    }
}

mod bar {
    pub fn do_other_stuff() {
        tracing::debug!("doing some different stuff...")
        // ...
    }
}

// Build a line filter to enable the events in `do_stuff`
// and `do_other_stuff`.
let mut filter = LineFilter::default();
filter.with_modules(vec![
   ("my_crate::foo", 5),
   ("my_crate::bar", 12)
]);

// Build a subscriber and enable that filter.
use tracing_subscriber::prelude::*;

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

// Now, the events are enabled!
foo::do_stuff();
bar::do_other_stuff();

pub fn with_files<I>(
    &mut self,
    files: impl IntoIterator<Item = (I, u32)>
) -> Result<&mut Self, BadPath> where
    I: AsRef<Path>, 
[src]

Enable a set of spans or events by file path.

This is equivalent to repeatedly calling [enable_by_file], and follows the same path validation rules as that method. See the documentation for [enable_by_file] for details.

Trait Implementations

impl Debug for LineFilter[src]

impl Default for LineFilter[src]

impl<S: Subscriber> Layer<S> for LineFilter where
    EnvFilter: Layer<S>, 
[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.