fix: log correct response body size in logging middleware

Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-25 19:13:19 -03:00
parent 6ee1c64080
commit d03712874b

View File

@@ -206,7 +206,7 @@ pub async fn logging_middleware(
) -> Response {
let method = request.method().clone();
let uri = request.uri().clone();
let content_length = request.headers()
let request_content_length = request.headers()
.get("content-length")
.and_then(|v| v.to_str().ok())
.and_then(|s| s.parse::<u64>().ok())
@@ -216,7 +216,14 @@ pub async fn logging_middleware(
let response = next.run(request).await;
let duration = start.elapsed();
info!("{} {} {} {} {} bytes - {:?}", addr, method, uri, response.status(), content_length, duration);
// Try to get response body size from content-length header, or default to 0
let response_content_length = response.headers()
.get("content-length")
.and_then(|v| v.to_str().ok())
.and_then(|s| s.parse::<u64>().ok())
.unwrap_or(0);
info!("{} {} {} {} {} bytes - {:?}", addr, method, uri, response.status(), response_content_length, duration);
response
}