How to Use Structured Logging in Rust with tracing

Initialize the tracing crate with a subscriber to log structured events as key-value pairs in Rust.

Add the tracing crate to your dependencies and initialize a subscriber in main to capture structured logs.

use tracing::{info, Level};
use tracing_subscriber;

fn main() {
    tracing_subscriber::fmt()
        .with_max_level(Level::INFO)
        .init();

    info!(user_id = 42, "User logged in");
}

Add this to Cargo.toml:

[dependencies]
tracing = "0.1"
tracing-subscriber = "0.3"