I'm trying to set up logging in an Axum app, I have the logging working but the thing that bothers me is the need to do error("OHNOES SOMETHING WENT WRONG")
in every single error branch.
I did see #[instrument(err)]
in the documentation but A I'm trying to avoid having to need to put #[instrument(err)]
on every route handler and B I'm not sure that would work any way since Axum want's a Response
not a Result
to be returned from a given handler.
I did try to setup a stupidly simple error logging middleware:
#[instrument(name = "request")]
pub async fn main_response_mapper(res: Response) -> Response {
if res.status() != StatusCode::OK {
error!("{:?}", res.body());
}
res
}
But the body only reports with: UnsyncBoxBody
, (something to do with needing to support infinite data streams) so I can't use a middleware for this.
Is there a way to have the logging automatically pickup that it got an error and log out its message? (the error implements std::fat::Display
)