Init
This commit is contained in:
+3
-1
@@ -79,7 +79,9 @@ impl IntoResponse for HTTPError {
|
||||
.into_response();
|
||||
}
|
||||
HTTPError::Internal(err) => {
|
||||
tracing::error!(error = ?err, "An unexpected error occurred");
|
||||
// On utilise %err pour un message d'erreur clair sans backtrace brute
|
||||
// mais on garde les détails pour le span tracing si besoin.
|
||||
tracing::error!(%err, "Request error");
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, "Internal server error")
|
||||
}
|
||||
};
|
||||
|
||||
+25
-3
@@ -1,5 +1,5 @@
|
||||
use axum::{
|
||||
body::Body,
|
||||
body::{Body, HttpBody},
|
||||
extract::State,
|
||||
http::{header, Request},
|
||||
middleware::Next,
|
||||
@@ -55,8 +55,30 @@ pub async fn request_context_middleware(mut req: Request<Body>, next: Next) -> R
|
||||
);
|
||||
|
||||
async move {
|
||||
info!("Incoming request");
|
||||
next.run(req).await
|
||||
let response = next.run(req).await;
|
||||
let elapsed = started_at.elapsed();
|
||||
let status = response.status();
|
||||
|
||||
let size = response
|
||||
.body()
|
||||
.size_hint()
|
||||
.exact()
|
||||
.map(|s| s.to_string())
|
||||
.or_else(|| {
|
||||
response
|
||||
.headers()
|
||||
.get(header::CONTENT_LENGTH)
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.map(|s| s.to_string())
|
||||
})
|
||||
.unwrap_or_else(|| "0".to_string());
|
||||
|
||||
if status.is_server_error() {
|
||||
tracing::error!(%status, %size, ?elapsed, "Request failed");
|
||||
} else {
|
||||
info!("{} {}b in {:?}", status, size, elapsed);
|
||||
}
|
||||
response
|
||||
}
|
||||
.instrument(span)
|
||||
.await
|
||||
|
||||
Reference in New Issue
Block a user