summaryrefslogtreecommitdiff
path: root/middleware/logger.go
blob: 039bd1929d73155ed9e06ffdda5c0870699eb95c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package middleware

import (
	"net/http"

	"log/slog"
)

func Logger(logger *slog.Logger) Middleware {
	return func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			next.ServeHTTP(w, r)
			requestID := GetRequestID(r)
			args := []any{
				slog.String("proto", r.Proto),
				slog.String("method", r.Method),
				slog.String("request_uri", r.RequestURI),
			}
			if requestID != "" {
				args = append(args, slog.String("request_id", requestID))
			}
			logger.InfoContext(
				r.Context(),
				"request",
				args...,
			)
		})
	}
}