-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.go
More file actions
61 lines (51 loc) · 1.49 KB
/
logging.go
File metadata and controls
61 lines (51 loc) · 1.49 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package api
import (
"log/slog"
"net/http"
"time"
)
// responseRecorder wraps http.ResponseWriter to capture the status code and size.
type responseRecorder struct {
http.ResponseWriter
status int
size int
}
func (r *responseRecorder) WriteHeader(code int) {
r.status = code
r.ResponseWriter.WriteHeader(code)
}
func (r *responseRecorder) Write(b []byte) (int, error) {
n, err := r.ResponseWriter.Write(b)
r.size += n
return n, err
}
// Unwrap returns the underlying ResponseWriter (supports http.ResponseController).
func (r *responseRecorder) Unwrap() http.ResponseWriter {
return r.ResponseWriter
}
// Logger returns middleware that logs each request using the provided slog.Logger.
func Logger(logger *slog.Logger) Middleware {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
rec := &responseRecorder{ResponseWriter: w, status: http.StatusOK}
next.ServeHTTP(rec, r)
attrs := []slog.Attr{
slog.String("method", r.Method),
slog.String("path", r.URL.Path),
slog.Int("status", rec.status),
slog.Duration("latency", time.Since(start)),
slog.Int("size", rec.size),
slog.String("remote", r.RemoteAddr),
}
if id := GetRequestID(r); id != "" {
attrs = append(attrs, slog.String("request_id", id))
}
args := make([]any, len(attrs))
for i, a := range attrs {
args[i] = a
}
logger.LogAttrs(r.Context(), slog.LevelInfo, "request", attrs...)
})
}
}