6

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"] }
kmdreko
  • 42,554
  • 6
  • 57
  • 106
aedm
  • 5,596
  • 2
  • 32
  • 36
  • 1
    The `tracing`-crate is only concerned with producing events; some other crate is tasked with generating actual logs from those events. That crate (e.g. `tracing-subscriber`) will have knobs and switches to filter log events. You may want to update your question wrt that, e.g. posting your cargo manifest. – user2722968 Aug 05 '22 at 09:47
  • Thanks, that's already very helpful. So if I understand correctly, events are emitted regardless of the verbosity levels of either the events or the spans. And it's the subscriber that needs to filter those events, right? I added details to the question. – aedm Aug 05 '22 at 10:07
  • 2
    In that case, see [here](https://docs.rs/tracing-subscriber/0.3.15/tracing_subscriber/fmt/index.html#filtering-events-with-environment-variables) and [here](https://docs.rs/env_logger/latest/env_logger/#enabling-logging) on how to filter logging output to just your crate/module – user2722968 Aug 05 '22 at 10:12

1 Answers1

5

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.

kmdreko
  • 42,554
  • 6
  • 57
  • 106
  • this means that you need to explicitly whitelist every tracing target in your project, right? The default behavior is one target per module, so you would need to configure hundreds of such directives. – Derek Thurn Jan 22 '23 at 23:32
  • @DerekThurn The above with `=none` prefixed would imply a whitelist mechanism, yes. But you don't have to list every *module* you intend to allow, only every *crate*. I understood OP desired only wanted to see events from their own crate(s) so a whitelist would work better. You could flip it to be like a blacklist if you want: `=debug,h2=error` (allow all debug events, except only errors from the h2 crate). – kmdreko Jan 23 '23 at 02:15