1package spindle
2
3import (
4 "log/slog"
5 "net/http"
6 "time"
7)
8
9func (s *Spindle) RequestLogger(next http.Handler) http.Handler {
10 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
11 start := time.Now()
12
13 next.ServeHTTP(w, r)
14
15 // Build query params as slog.Attrs for the group
16 queryParams := r.URL.Query()
17 queryAttrs := make([]any, 0, len(queryParams))
18 for key, values := range queryParams {
19 if len(values) == 1 {
20 queryAttrs = append(queryAttrs, slog.String(key, values[0]))
21 } else {
22 queryAttrs = append(queryAttrs, slog.Any(key, values))
23 }
24 }
25
26 s.l.LogAttrs(r.Context(), slog.LevelInfo, "",
27 slog.Group("request",
28 slog.String("method", r.Method),
29 slog.String("path", r.URL.Path),
30 slog.Group("query", queryAttrs...),
31 slog.Duration("duration", time.Since(start)),
32 ),
33 )
34 })
35}