Trying to figure out how tracing filters work

⚓ Rust    📅 2025-11-05    👤 surdeus    👁️ 5      

surdeus

I wrote a test program to understand tracing filters:

use log::{debug, error, info, warn};
use tracing::{debug_span, info_span};
use tracing_subscriber::{prelude::*, EnvFilter, fmt};

fn main() {
    tracing_subscriber::registry()
        .with(fmt::layer().pretty())
        .with(EnvFilter::from_default_env())
        .init();

    info!("This is a global info event.");

    let outer = info_span!("outer_operation");
    let _outer = outer.enter();
    info!("Entered outer span.");

    {
        let alice = info_span!(
            "user_action",
            user = "alice",
            status = "success"
        );
        let _alice = alice.enter();
        info!("Processing action for alice.");
        debug!("Debug message inside alice span.");
    }


    {
        let bob = info_span!(
            "user_action",
            user = "bob",
            status = "error"
        );
        let _bob = bob.enter();
        warn!("Warning for bob's action.");
    }

    {
        let process = info_span!(
            "processing_step",
            msg = "error in step 2"
        );
        let _process = process.enter();
        error!("Error occurred in processing.");
    }

    {
        let count_span = info_span!(
            "count_operation",
            count = 42i64
        );
        let _count = count_span.enter();
        info!("Counting to 42.");
    }
}

Then I tried to disable all user_action logs using an env filter:

 RUST_LOG='info,[user_action]=error' cargo run --bin test

But it didn't work. Logs for bob and alice were still printed. How do I do this?

I've been unable to find a good guide for tracing that goes into advanced features like filtering beyond the basics? Does something like this exist?

1 post - 1 participant

Read full topic

🏷️ Rust_feed