Skip to content

Commit c1103b5

Browse files
authored
feat(go): log requests on info with duration (#805)
1 parent 1858c2c commit c1103b5

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

pkg/api/middleware/logger.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@ import (
44
"log/slog"
55
"net/http"
66
"sync"
7+
"time"
78
)
89

910
func RequestLogger(h http.Handler) http.Handler {
1011
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
12+
start := time.Now()
1113
lw := &loggingResponseWriter{w: w}
1214
defer func() {
13-
slog.DebugContext(r.Context(), "request complete",
15+
slog.InfoContext(r.Context(), "request complete",
1416
"method", r.Method,
1517
"mux_pattern", r.Pattern,
1618
"uri", r.URL,
1719
"status", lw.statusCode,
18-
"status_text", http.StatusText(lw.statusCode))
20+
"status_text", http.StatusText(lw.statusCode),
21+
"duration", time.Since(start))
1922
}()
2023
h.ServeHTTP(lw, r)
2124
})

0 commit comments

Comments
 (0)