tracing_mock::layer

Function mock

Source
pub fn mock() -> MockLayerBuilder
Expand description

Create a MockLayerBuilder used to construct a MockLayer.

For additional information and examples, see the layer module and MockLayerBuilder documentation.

ยงExamples

use tracing_mock::{expect, layer};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};

let span = expect::span()
    .named("my_span");
let (layer, handle) = layer::mock()
    // Enter a matching span
    .enter(&span)
    // Record an event with message "collect parting message"
    .event(expect::event().with_fields(expect::msg("say hello")))
    // Exit a matching span
    .exit(&span)
    // Expect no further messages to be recorded
    .only()
    // Return the subscriber and handle
    .run_with_handle();

// Use `set_default` to apply the `MockSubscriber` until the end
// of the current scope (when the guard `_subscriber` is dropped).
let _subscriber =  tracing_subscriber::registry()
    .with(layer.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
    .set_default();

{
    let span = tracing::trace_span!(
        "my_span",
        greeting = "hello world",
    );

    let _guard = span.enter();
    tracing::info!("say hello");
}

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