package server import ( "log/slog" "net/http" "time" ) type statusRecorder struct { http.ResponseWriter status int } func (r *statusRecorder) WriteHeader(code int) { r.ResponseWriter.WriteHeader(code) r.status = code } func logRequests(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { start := time.Now() recorder := &statusRecorder{ResponseWriter: w, status: http.StatusOK} next.ServeHTTP(recorder, r) slog.Info("request", "method", r.Method, "path", r.URL.Path, "status", recorder.status, "duration", time.Since(start), "remote", r.RemoteAddr, "x-forwarded-for", r.Header.Get("x-forwarded-for"), ) }) }