Skip to main content

log_middleware

Function log_middleware 

Source
pub async fn log_middleware(
    __arg0: ConnectInfo<Ipv4Addr>,
    request: Request,
    next: Next,
) -> impl IntoResponse
Expand description

Middleware for post-request error telemetry.

This middleware runs after the route handler has completed and inspects the response for a crate::logger::Entry that may have been inserted by crate::Error::into_response. If an entry is present and the global logger is active, it prepends the client IP address, HTTP method, and request path to the message and dispatches the entry to the logging service.

This function is only attached to the router when crate::logger::logging_enabled returns true. When the logger is inactive the entire middleware layer is absent from the router, so no overhead is incurred.

ยงDesign: zero-allocation on the happy path

The HTTP method (Method) and URI (Uri) are cloned before the handler runs. Both types are cheap to clone (Method is a small enum, Uri is backed by an Arc-managed buffer). The format string that prepends the request context to the log message is only allocated inside the if let block, which is only entered when an error entry is actually present in the response extensions.