How to Add Logging to a Rust Application

Add the `tracing` crate to your project and initialize a subscriber in `main` to capture logs.

How to Add Logging to a Rust Application

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

  1. Add the dependency to Cargo.toml:
[dependencies]
tracing = "0.1"
tracing-subscriber = "0.3"
  1. Import the macros and initialize the subscriber in src/main.rs:
use tracing::{info, Level};
use tracing_subscriber;

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

    info!("Application started");
}