How to Use Tracing for Async Debugging in Rust

Enable async debugging in Rust by adding the tracing crate and using the #[instrument] attribute to log function execution automatically.

Use the tracing crate with the #[tracing::instrument] attribute to automatically log entry, exit, and arguments for async functions. Add the dependency to your Cargo.toml and annotate your async function to capture execution flow.

[dependencies]
tracing = "0.1"
use tracing::instrument;

#[instrument(level = "debug")]
async fn my_async_task() {
    // Tracing automatically logs when this function starts and ends
    tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
}

Run your application with RUST_LOG=debug cargo run to see the trace output in the console.