🛈 Note: This is pre-release documentation for the upcoming tracing 0.2.0 ecosystem.

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

Crate tracing_mock

source ·
Expand description

Tracing — Structured, application-level diagnostics


Utilities for testing tracing and crates that uses it.

Documentation (master) MIT licensed Build Status Discord chat

Documentation | Chat


tracing is a framework for instrumenting Rust programs to collect structured, event-based diagnostic information. tracing-mock provides tools for making assertions about what tracing diagnostics are emitted by code under test.

Compiler support: requires rustc 1.49+


tracing-mock crate provides a mock Collector that allows asserting on the order and contents of spans and events.

As tracing-mock isn’t available on crates.io yet, you must import it via git. When using tracing-mock with the tracing 0.1 ecosystem, it is important that you also override the source of any tracing crates that are transient dependencies. For example, the Cargo.toml for your test crate could contain:

lib-under-test = "1.0" # depends on `tracing`

tracing-mock = { git = "https://github.com/tokio-rs/tracing", branch = "v0.1.x", version = "0.1" }
tracing = { git = "https://github.com/tokio-rs/tracing", branch = "v0.1.x", version = "0.1" }

tracing = { git = "https://github.com/tokio-rs/tracing", branch = "v0.1.x" }
tracing-core = { git = "https://github.com/tokio-rs/tracing", branch = "v0.1.x" }


The following examples are for the master branch. For examples that will work with tracing from [crates.io], please check the v0.1.x branch.

Below is an example that checks that an event contains a message:

use tracing::collect::with_default;
use tracing_mock::{collector, expect, field};

fn yak_shaving() {
    tracing::info!("preparing to shave yaks");

let (collector, handle) = collector::mock()
    .event(expect::event().with_fields(field::msg("preparing to shave yaks")))

with_default(collector, || {


Below is a slightly more complex example. tracing-mock asserts that, in order:

  • a span is created with a single field/value pair
  • the span is entered
  • an event is created with the field number_of_yaks, a corresponding value of 3, and the message “preparing to shave yaks”, and nothing else
  • an event is created with the field all_yaks_shaved, a corresponding value of true, and the message “yak shaving completed”
  • the span is exited
  • no further traces are received
use tracing::collect::with_default;
use tracing_mock::{collector, expect, field};

fn yak_shaving(number_of_yaks: u32) {
    tracing::info!(number_of_yaks, "preparing to shave yaks");

    let number_shaved = number_of_yaks; // shave_all
        all_yaks_shaved = number_shaved == number_of_yaks,
        "yak shaving completed."

let yak_count: u32 = 3;
let span = expect::span().named("yak_shaving");

let (collector, handle) = collector::mock()
                .and(field::msg("preparing to shave yaks"))
                .and(field::msg("yak shaving completed."))

with_default(collector, || {


Supported Rust Versions

Tracing is built against the latest stable release. The minimum supported version is 1.49. The current Tracing version is not guaranteed to build on Rust versions earlier than the minimum supported version.

Tracing follows the same compiler support policies as the rest of the Tokio project. The current stable Rust compiler and the three most recent minor versions before it will always be supported. For example, if the current stable compiler version is 1.45, the minimum supported version will not be increased past 1.42, three minor versions prior. Increasing the minimum supported compiler version is not considered a semver breaking change as long as doing so complies with this policy.


This project is licensed under the MIT license.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Tracing by you, shall be licensed as MIT, without any additional terms or conditions.


An ExpectedEvent defines an event to be matched by the mock collector API in the collector module.
An implementation of the Subscribe trait which validates that the tracing data it recieves matches the expected output for a test.