Logging the body of a request in a tracing layer #1798
-
I have a handler that expects a JSON body and responds with a 422 if the JSON can't be deserialized: async fn handle(
State(pool): State<DbPool>,
Json(push_message): Json<pubsub::PushMessage<MyType>>,
) -> Result<(), MyError> … and I want to trace the requests to this API. I can log requests, but the body will only log let app = Router::new()
.route("/", axum::routing::post(handle))
.layer(
TraceLayer::new(
tower_http::classify::StatusInRangeAsFailures::new(400..=599)
.into_make_classifier(),
)
.on_request(|request: &hyper::Request<axum::body::Body>, _: &'_ _| {
tracing::info!("Body: {:?}", request.body());
}),
) Is it possible to log the full body? Ideally, I would only do this when the JSON is rejected (and a 422 is returned), but doing it for all requests would also work for this API. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The Trace middleware doesn't support that. It's a bit complicated since the body is an async stream, so in order to log it you need to buffer the whole thing, which doesn't work for infinite streams and Trace needs to support those. You can roll your own tracing middleware pretty easily though and can consume the body like this https://github.com/tokio-rs/axum/blob/main/examples/consume-body-in-extractor-or-middleware/src/main.rs |
Beta Was this translation helpful? Give feedback.
The Trace middleware doesn't support that. It's a bit complicated since the body is an async stream, so in order to log it you need to buffer the whole thing, which doesn't work for infinite streams and Trace needs to support those.
You can roll your own tracing middleware pretty easily though and can consume the body like this https://github.com/tokio-rs/axum/blob/main/examples/consume-body-in-extractor-or-middleware/src/main.rs