Search code examples
rustrust-tracing

How to turn off tracing events emitted by other crates?


I use tracing, and I only want to see my own debug events. However, some crates I depend on also have tracing support, and they litter the event stream. So when I increase verbosity to DEBUG, I see a lot of these in my logs:

2022-08-04T20:52:24.523161Z DEBUG hyper::proto::h1::io: flushed 1008 bytes

I tried to turn off these events by adding spans around such calls:

let response = {
    let span = tracing::info_span!("my_span");
    let _guard = span.enter();
    client
        // set up the request
        .send()
        .await
};

I expected those 3rd party DEBUG events to go away since the span's verbosity level is INFO. But they remained. The docs of spans is a little light on what the verbosity level of a span really means, so my interpretation might be totally off.

How do I set the verbosity level for dependent crates, so only my own DEBUG events appear in the trace logs?

I set the verbosity level using the environment variable RUST_LOG=debug, and I set up tracing-subscriber like this:

tracing_subscriber::fmt::init();

Relevant part of Cargo.toml:

tracing = "0.1.36"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

Solution

  • Since you use tracing_subscriber::fmt::init() with the "env-filter" feature enabled so it reads from your environment, you already have all the pieces you need to filter the logs to your liking.

    You can simply set RUST_LOG like so (insert your own crate for "mycrate"):

    RUST_LOG=none,mycrate=debug
    

    This will disable logging for all tracing events, unless they come from "mycrate" which will be logged if they are at the DEBUG level or higher. You can read the documentation for EnvFilter Directives for more customization.