Learning Axum and I will like to add logging to a service I have put together, but unfortunately I cannot get it to work.
What I have done?
I added tower-http = { version = "0.3.5", features = ["trace"] }
to Cargo.html and in the definition of the service I have this:
use tower_http::trace::TraceLayer;
let app = Router::new()
.route("/:name/path", axum::routing::get(handler))
.layer(TraceLayer::new_for_http())
But when I start the application and make requests to the endpoint, nothing gets logged.
Is there any configuration step I could have missed?
** Edit 1 **
As suggested in the comment, I added the tracing-subscriber crate:
tracing-subscriber = { version = "0.3"}
and updated the code as follows:
use tower_http::trace::TraceLayer;
use tower_http::trace::TraceLayer;
tracing_subscriber::fmt().init();
let app = Router::new()
.route("/:name/path", axum::routing::get(handler))
.layer(TraceLayer::new_for_http())
But yet, still no log output.
** Edit 2 **
Ok so what finally worked was
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
and then initilize as follows:
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer())
.init();
Even though this get the log output, I cannot explain why. So perhaps someone who understands how these crates work can give an explanation which I can accept as the answer to the question