TracingCollector

Struct TracingCollector 

Source
pub struct TracingCollector { /* private fields */ }
Expand description

TracingCollector creates a tracing subscriber that collects a copy of all traces into a buffer. These traces can be retrieved by calling its Display implementation, i.e. calling log.to_string() or format!("{log}"). This is useful for testing with insta snapshots.

IMPORTANT! TracingCollector is meant for use when testing. It collects logs into a memory buffer which keeps growing until it is read, the program exits or it is dropped. This means that if you are using TracingCollector in production the program will eventually run out of memory.

When the TracingCollector is dropped, the buffer is emptied and the tracing subscriber is released but the memory equivalent of a Mutex and an empty Vec is leaked.

When reading the traces, they are stripped of ANSI escape codes and prefixed with a character. The former allows the use of colored & formatted terminal output when the test fails or is run with --nocapture and the latter makes the insta inline snapshots work since rust’s r### raw string literals strips leading whitespace. The prefix can be changed or removed using the set_prefix and remove_prefix methods.

Example:

#[test]
fn test_logs() {
    let log = TracingCollector::init_debug_level();
    tracing::info!("First log");

    insta::assert_display_snapshot!(log, @r###"
    ㏒   INFO  First log
        at tests/test.rs:6

    "###);

    tracing::debug!("Second log");
    tracing::info!("Third log");

    insta::assert_display_snapshot!(log, @r###"
    ㏒  DEBUG  Second log
        at tests/test.rs:14

      INFO  Third log
       at tests/test.rs:15

   "###);
}

Implementations§

Source§

impl TracingCollector

Source

pub fn set_prefix(&mut self, prefix: char)

Source

pub fn remove_prefix(&mut self)

Source

pub fn init_trace_level() -> Self

Create a TracingCollector that collects traces up to the TRACE level.

Source

pub fn init_debug_level() -> Self

Create a TracingCollector that collects traces up to the DEBUG level.

Source

pub fn init_info_level() -> Self

Create a TracingCollector that collects traces up to the INFO level.

Source

pub fn init(max_level: Level) -> Self

Create a new TracingCollector that collects traces up to the specified level.

Source

pub fn clear(&self)

Trait Implementations§

Source§

impl Display for TracingCollector

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Drop for TracingCollector

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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> 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> 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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more