Skip to main content
Lightning ships two middleware functions out of the box: 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:

Register Logger

Example output

Recovery

Recovery() wraps the downstream handler chain in a defer/recover block. When a panic occurs, it:
  1. Writes the panic value and full stack trace to stderr.
  2. 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 single HandlerFunc 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:
This is exactly equivalent to:

Composing middleware manually

When you need DefaultApp-style setup but also want a custom Config, use NewApp and call app.Use yourself:
Middleware runs in the order you register it. Because Logger calls ctx.Next() before writing its log line, placing Logger before Recovery means the log entry correctly reflects any status code set by the panic handler. Register Logger first, then Recovery.