๐Ÿ›ˆ Note: This is pre-release documentation for the upcoming tracing 0.2.0 ecosystem.

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

tracing

Struct Level

Source
pub struct Level(/* private fields */);
Expand description

Describes the level of verbosity of a span or event.

ยงComparing Levels

Level implements the PartialOrd and Ord traits, allowing two Levels to be compared to determine which is considered more or less verbose. Levels which are more verbose are considered โ€œgreater thanโ€ levels which are less verbose, with Level::ERROR considered the lowest, and Level::TRACE considered the highest.

For example:

use tracing_core::Level;

assert!(Level::TRACE > Level::DEBUG);
assert!(Level::ERROR < Level::WARN);
assert!(Level::INFO <= Level::DEBUG);
assert_eq!(Level::TRACE, Level::TRACE);

ยงFiltering

Levels are typically used to implement filtering that determines which spans and events are enabled. Depending on the use case, more or less verbose diagnostics may be desired. For example, when running in development, DEBUG-level traces may be enabled by default. When running in production, only INFO-level and lower traces might be enabled. Libraries may include very verbose diagnostics at the DEBUG and/or TRACE levels. Applications using those libraries typically chose to ignore those traces. However, when debugging an issue involving said libraries, it may be useful to temporarily enable the more verbose traces.

The LevelFilter type is provided to enable filtering traces by verbosity. Levels can be compared against LevelFilters, and LevelFilter has a variant for each Level, which compares analogously to that level. In addition, LevelFilter adds a LevelFilter::OFF variant, which is considered โ€œless verboseโ€ than every other Level. This is intended to allow filters to completely disable tracing in a particular context.

For example:

use tracing_core::{Level, LevelFilter};

assert!(LevelFilter::OFF < Level::TRACE);
assert!(LevelFilter::TRACE > Level::DEBUG);
assert!(LevelFilter::ERROR < Level::WARN);
assert!(LevelFilter::INFO <= Level::DEBUG);
assert!(LevelFilter::INFO >= Level::INFO);

ยงExamples

Below is a simple example of how a collector could implement filtering through a LevelFilter. When a span or event is recorded, the Collect::enabled method compares the span or eventโ€™s Level against the configured LevelFilter. The optional Collect::max_level_hint method can also be implemented to allow spans and events above a maximum verbosity level to be skipped more efficiently, often improving performance in short-lived programs.

use tracing_core::{span, Event, Level, LevelFilter, Collect, Metadata};

#[derive(Debug)]
pub struct MyCollector {
    /// The most verbose level that this collector will enable.
    max_level: LevelFilter,

    // ...
}

impl MyCollector {
    /// Returns a new `MyCollector` which will record spans and events up to
    /// `max_level`.
    pub fn with_max_level(max_level: LevelFilter) -> Self {
        Self {
            max_level,
            // ...
        }
    }
}
impl Collect for MyCollector {
    fn enabled(&self, meta: &Metadata<'_>) -> bool {
        // A span or event is enabled if it is at or below the configured
        // maximum level.
        meta.level() <= &self.max_level
    }

    // This optional method returns the most verbose level that this
    // collector will enable. Although implementing this method is not
    // *required*, it permits additional optimizations when it is provided,
    // allowing spans and events above the max level to be skipped
    // more efficiently.
    fn max_level_hint(&self) -> Option<LevelFilter> {
        Some(self.max_level)
    }

    // Implement the rest of the collector...
    fn new_span(&self, span: &span::Attributes<'_>) -> span::Id {
        // ...
    }

    fn event(&self, event: &Event<'_>) {
        // ...
    }

    // ...
}

It is worth noting that the tracing-subscriber crate provides additional APIs for performing more sophisticated filtering, such as enabling different levels based on which module or crate a span or event is recorded in.

Implementationsยง

Sourceยง

impl Level

Source

pub const ERROR: Level

The โ€œerrorโ€ level.

Designates very serious errors.

Source

pub const WARN: Level

The โ€œwarnโ€ level.

Designates hazardous situations.

Source

pub const INFO: Level

The โ€œinfoโ€ level.

Designates useful information.

Source

pub const DEBUG: Level

The โ€œdebugโ€ level.

Designates lower priority information.

Source

pub const TRACE: Level

The โ€œtraceโ€ level.

Designates very low priority, often extremely verbose, information.

Source

pub fn as_str(&self) -> &'static str

Returns the string representation of the Level.

This returns the same string as the fmt::Display implementation.

Trait Implementationsยง

Sourceยง

impl Clone for Level

Sourceยง

fn clone(&self) -> Level

Returns a copy of the value. Read more
1.0.0 ยท Sourceยง

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Sourceยง

impl Debug for Level

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Sourceยง

impl Display for Level

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Sourceยง

impl From<Level> for LevelFilter

Sourceยง

fn from(level: Level) -> LevelFilter

Converts to this type from the input type.
Sourceยง

impl FromStr for Level

Sourceยง

type Err = ParseLevelError

The associated error which can be returned from parsing.
Sourceยง

fn from_str(s: &str) -> Result<Level, ParseLevelError>

Parses a string s to return a value of this type. Read more
Sourceยง

impl Hash for Level

Sourceยง

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 ยท Sourceยง

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Sourceยง

impl Ord for Level

Sourceยง

fn cmp(&self, other: &Level) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 ยท Sourceยง

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 ยท Sourceยง

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 ยท Sourceยง

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Sourceยง

impl PartialEq<Level> for LevelFilter

Sourceยง

fn eq(&self, other: &Level) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 ยท Sourceยง

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Sourceยง

impl PartialEq<LevelFilter> for Level

Sourceยง

fn eq(&self, other: &LevelFilter) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 ยท Sourceยง

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Sourceยง

impl PartialEq for Level

Sourceยง

fn eq(&self, other: &Level) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 ยท Sourceยง

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Sourceยง

impl PartialOrd<Level> for LevelFilter

Sourceยง

fn partial_cmp(&self, other: &Level) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
Sourceยง

fn lt(&self, other: &Level) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
Sourceยง

fn le(&self, other: &Level) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
Sourceยง

fn gt(&self, other: &Level) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
Sourceยง

fn ge(&self, other: &Level) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Sourceยง

impl PartialOrd<LevelFilter> for Level

Sourceยง

fn partial_cmp(&self, other: &LevelFilter) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
Sourceยง

fn lt(&self, other: &LevelFilter) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
Sourceยง

fn le(&self, other: &LevelFilter) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
Sourceยง

fn gt(&self, other: &LevelFilter) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
Sourceยง

fn ge(&self, other: &LevelFilter) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Sourceยง

impl PartialOrd for Level

Sourceยง

fn partial_cmp(&self, other: &Level) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
Sourceยง

fn lt(&self, other: &Level) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
Sourceยง

fn le(&self, other: &Level) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
Sourceยง

fn gt(&self, other: &Level) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
Sourceยง

fn ge(&self, other: &Level) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Sourceยง

impl Copy for Level

Sourceยง

impl Eq for Level

Sourceยง

impl StructuralPartialEq for Level

Auto Trait Implementationsยง

Blanket Implementationsยง

Sourceยง

impl<T> Any for T
where T: 'static + ?Sized,

Sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Sourceยง

impl<T> Borrow<T> for T
where T: ?Sized,

Sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Sourceยง

impl<T> BorrowMut<T> for T
where T: ?Sized,

Sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Sourceยง

impl<T> CloneToUninit for T
where T: Clone,

Sourceยง

unsafe fn clone_to_uninit(&self, dst: *mut u8)

๐Ÿ”ฌThis is a nightly-only experimental API. (clone_to_uninit #126799)
Performs copy-assignment from self to dst. Read more
Sourceยง

impl<T> From<T> for T

Sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

Sourceยง

impl<T> Instrument for T

Sourceยง

fn instrument(self, span: Span) -> Instrumented<Self> โ“˜

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Sourceยง

fn in_current_span(self) -> Instrumented<Self> โ“˜

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Sourceยง

impl<T, U> Into<U> for T
where U: From<T>,

Sourceยง

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Sourceยง

impl<T> ToOwned for T
where T: Clone,

Sourceยง

type Owned = T

The resulting type after obtaining ownership.
Sourceยง

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Sourceยง

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Sourceยง

impl<T> ToString for T
where T: Display + ?Sized,

Sourceยง

fn to_string(&self) -> String

Converts the given value to a String. Read more
Sourceยง

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Sourceยง

type Error = Infallible

The type returned in the event of a conversion error.
Sourceยง

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Sourceยง

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Sourceยง

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

The type returned in the event of a conversion error.
Sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Sourceยง

impl<T> WithCollector for T

Sourceยง

fn with_collector<C>(self, collector: C) -> WithDispatch<Self> โ“˜
where C: Into<Dispatch>,

Available on crate feature std only.
Attaches the provided collector to this type, returning a WithDispatch wrapper. Read more
Sourceยง

fn with_current_collector(self) -> WithDispatch<Self> โ“˜

Available on crate feature std only.
Attaches the current default collector to this type, returning a WithDispatch wrapper. Read more