Logger for request logging and Recovery for panic recovery. You can register them individually or get both at once via DefaultApp.
Logger
Logger() logs a single line for every completed request. It calls ctx.Next() first, so the log entry is written after the handler finishes, which means the status code and duration are accurate.
Each log line contains the following fields in order:
| Field | Example |
|---|---|
| Remote address | 127.0.0.1:54321 |
| HTTP method | GET |
| Status code | 200 |
| Path | /api/users |
| Duration | 3ms |
| User agent | curl/8.7.1 |
Register Logger
Example output
Recovery
Recovery() wraps the downstream handler chain in a defer/recover block. When a panic occurs, it:
- Writes the panic value and full stack trace to
stderr. - Responds to the client with
HTTP 500 Internal Server Error.
Register Recovery with the default handler
Register Recovery with a custom panic handler
Pass a singleHandlerFunc to Recovery to replace the default 500 response with your own logic:
The panic value and stack trace are always written to
stderr by Recovery, regardless of whether you supply a custom handler. Your custom handler only controls the HTTP response sent to the client.Custom handler that logs to a structured logger
If your application uses a structured logger, you can capture the panic context and forward it before writing the response:DefaultApp
DefaultApp() is the quickest way to get a fully configured application with both middleware already registered:
Composing middleware manually
When you needDefaultApp-style setup but also want a custom Config, use NewApp and call app.Use yourself: