Chi Middleware Request Logging
Log every request with chi middleware: request ids, durations, status codes, and route patterns instead of raw paths.
Code
package main
import (
"fmt"
"log/slog"
"net/http"
"os"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
func main() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelInfo,
}))
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(requestLogger(logger))
r.Use(middleware.Recoverer)
r.Get("/orders/{orderID}", func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "orderID")
logger.InfoContext(r.Context(), "order fetched",
slog.String("order_id", id),
)
w.Write([]byte("order " + id))
})
http.ListenAndServe(":8080", r)
}
func requestLogger(logger *slog.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
started := time.Now()
wrapped := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
defer func() {
logger.LogAttrs(r.Context(), slog.LevelInfo, "request",
slog.String("request_id", middleware.GetReqID(r.Context())),
slog.String("method", r.Method),
slog.String("route", chi.RouteContext(r.Context()).RoutePattern()),
slog.Int("status", wrapped.Status()),
slog.Int("bytes", wrapped.BytesWritten()),
slog.Duration("duration", time.Since(started)),
)
}()
next.ServeHTTP(wrapped, r)
})
}
}How it works
The acting surface is the function itselfs main and requestLogger — everything else is wiring.
The cleanup call is deferred to function exit — success, early return, and panic all covered.
What to watch for
- Requires
github.com/go-chi/chi/v5andgithub.com/go-chi/chi/v5/middlewareas packages; the snippet will not run without them.
Using it
What the file centers on is func main()
The entry point wires requestLogger together — run it once and the whole demo flows.
Interactive Tools for This Topic
HTTP Request Builder & cURLBuild HTTP requests with method, URL, headers, and body, then generate the equivalent cURL command. Supports GET, POST, PUT, DELETE, and PATCH with custom headers.HTTP Retry & Backoff CalculatorSimulate exponential backoff retry schedules with Full, Equal, Decorrelated, or No Jitter to inspect per-attempt wait times and cumulative latency.
FAQ
- What does this need from the package registry?
- These are real dependencies: `github.com/go-chi/chi/v5` and `github.com/go-chi/chi/v5/middleware`. Without them the snippet will not load.
Report a problem with this exampleOpens a pre-filled issue on GitHub.