logging.go 410 B

123456789101112131415161718
  1. package middleware
  2. import (
  3. "log/slog"
  4. "net/http"
  5. )
  6. // RequestLogger logs each request with method, path, and raw query string.
  7. func RequestLogger(logger *slog.Logger, next http.Handler) http.Handler {
  8. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  9. logger.Info("request",
  10. "method", r.Method,
  11. "path", r.URL.Path,
  12. "query", r.URL.RawQuery,
  13. )
  14. next.ServeHTTP(w, r)
  15. })
  16. }