log api error in one self-sufficient line (fix #1381) (#1390)

this makes it more easy to correlate an error with the request that caused it. This can be helpful during debugging, or when setting up some sort of automation based on log content

Reviewed-on: https://git.deuxfleurs.fr/Deuxfleurs/garage/pulls/1390
Reviewed-by: Alex <lx@deuxfleurs.fr>
Reviewed-by: maximilien <git@mricher.fr>
Co-authored-by: trinity-1686a <trinity@deuxfleurs.fr>
Co-committed-by: trinity-1686a <trinity@deuxfleurs.fr>
This commit is contained in:
trinity-1686a
2026-03-20 20:22:34 +00:00
committed by Alex
parent 96b986a0a0
commit 8341b7f914

View File

@@ -163,10 +163,11 @@ impl<A: ApiHandler> ApiServer<A> {
.map(|k| format!("(key {k}) "))
.unwrap_or_default();
let method = req.method().clone();
if A::API_NAME == "admin" && (uri.path() == "/health" || uri.path() == "/metrics") {
debug!("{source} {key}{} {uri}", req.method());
debug!("{source} {key}{method} {uri}");
} else {
info!("{source} {key}{} {uri}", req.method());
info!("{source} {key}{method} {uri}");
}
debug!("{:?}", req);
@@ -202,9 +203,17 @@ impl<A: ApiHandler> ApiServer<A> {
let http_error = http_error_builder.body(body)?;
if e.http_status_code().is_server_error() {
warn!("Response: error {}, {}", e.http_status_code(), e);
warn!(
"error {}, {} in response to {source} {key}{method} {uri}",
e.http_status_code(),
e
);
} else {
info!("Response: error {}, {}", e.http_status_code(), e);
info!(
"error {}, {} in response to {source} {key}{method} {uri}",
e.http_status_code(),
e
);
}
Ok(http_error
.map(|body| BoxBody::new(body.map_err(|_: Infallible| unreachable!()))))