Minimal logging setup
axum uses the tracing and tracing-subscriber for logging so we need to include both.
[package]
name = "example-minimal-tracing"
version = "0.1.0"
edition = "2021"
publish = false
[dependencies]
axum = { path = "../../axum", features = ["tracing"] }
tokio = { version = "1.0", features = ["full"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
//! Run with //! //! ```not_rust //! cargo run -p example-minimal-tracing //! ``` use axum::{response::Html, routing::get, Router}; use tokio::net::TcpListener; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; #[tokio::main] async fn main() { // minimal setup for logging tracing_subscriber::registry() .with(tracing_subscriber::fmt::layer()) .init(); let app = app(); let listener = TcpListener::bind("127.0.0.1:3000").await.unwrap(); tracing::debug!("listening on {}", listener.local_addr().unwrap()); axum::serve(listener, app).await.unwrap(); } fn app() -> Router { Router::new().route("/", get(handler)) } async fn handler() -> Html<&'static str> { tracing::debug!("in handler"); Html("<h1>Hello, World!</h1>") }
When we start the application with cargo run we'll see line like this on the terminal:
2025-03-17T08:39:04.089621Z DEBUG example_minimal_tracing: listening on 127.0.0.1:3000
When we access the main page with a browser we'll see two more lines:
2025-03-17T08:39:27.044996Z TRACE axum::serve: connection 127.0.0.1:58560 accepted
2025-03-17T08:39:27.045345Z DEBUG example_minimal_tracing: in handler