Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Tracing in other crates

  • First enable tracing and observe the crate names it uses for tracing.
  • Then enable the configuration in which we can set the level of tracing for each crate.
  • Instead of using the name of our crate hard-coded we can get it from Cargo.toml using env!("CARGO_CRATE_NAME").
[package]
name = "demo"
version = "0.1.0"
edition = "2024"

[dependencies]
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
which = { version = "8.0.2", features = ["tracing"] }
use tracing::{debug, error, info, trace, warn};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

use which::which;

fn main() {
    tracing_subscriber::registry()
        //.with(
        //    tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
        //        format!(
        //            "{}=trace,which::checker=debug,which::finder=trace",
        //            env!("CARGO_CRATE_NAME")
        //        )
        //        .into()
        //    }),
        //)
        .with(tracing_subscriber::fmt::layer())
        .init();

    trace!("trace level");
    debug!("debug level");
    info!("info level");
    warn!("warn level");
    error!("error level");

    let _result = which("rustc").unwrap();
}