How to Add Logging to a Rust Application
Add the tracing crate to your project and initialize a subscriber in main to capture logs.
- Add the dependency to
Cargo.toml:
[dependencies]
tracing = "0.1"
tracing-subscriber = "0.3"
- 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");
}