package server import ( "log/slog" "net/http" "time" ) // responseWriter wraps http.ResponseWriter to capture the status code. type responseWriter struct { http.ResponseWriter statusCode int } // WriteHeader captures the status code. func (rw *responseWriter) WriteHeader(code int) { rw.statusCode = code rw.ResponseWriter.WriteHeader(code) } // loggingMiddleware wraps an http.Handler to log request details. func loggingMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { wrapped := &responseWriter{ResponseWriter: w} next.ServeHTTP(wrapped, r) start := time.Now() duration := time.Since(start) if wrapped.statusCode == 0 { wrapped.statusCode = 200 } slog.Info( "", slog.String("verb", r.Method), slog.String("path", r.URL.Path), slog.Uint64("code", uint64(wrapped.statusCode)), slog.Float64("duration", float64(duration.Nanoseconds())/1_000_000), ) }) }