tracing_mock::subscriber

Function mock

Source
pub fn mock() -> MockSubscriber<fn(_: &Metadata<'_>) -> bool>
Expand description

Create a new MockSubscriber.

For additional information and examples, see the subscriber module and MockSubscriber documentation.

ยงExamples

use tracing_mock::{expect, subscriber, field};

let span = expect::span()
    .named("my_span");
let (subscriber, handle) = subscriber::mock()
    // Enter a matching span
    .enter(&span)
    // Record an event with message "subscriber parting message"
    .event(expect::event().with_fields(expect::msg("subscriber parting message")))
    // Record a value for the field `parting` on a matching span
    .record(&span, expect::field("parting").with_value(&"goodbye world!"))
    // Exit a matching span
    .exit(span)
    // Expect no further messages to be recorded
    .only()
    // Return the subscriber and handle
    .run_with_handle();

// Use `with_default` to apply the `MockSubscriber` for the duration
// of the closure - this is what we are testing.
tracing::subscriber::with_default(subscriber, || {
    let span = tracing::trace_span!(
        "my_span",
        greeting = "hello world",
        parting = tracing::field::Empty
    );

    let _guard = span.enter();
    tracing::info!("subscriber parting message");
    let parting = "goodbye world!";

    span.record("parting", &parting);
});

// Use the handle to check the assertions. This line will panic if an
// assertion is not met.
handle.assert_finished();