Files
spend-sparrow/internal/handler/middleware/logger.go
Tim Wundenberg 661a3ba79f
Some checks failed
Build Docker Image / Build-Docker-Image (push) Failing after 4m29s
fix(observabillity): include otel logs
2025-06-07 15:20:16 +02:00

37 lines
679 B
Go

package middleware
import (
"log/slog"
"net/http"
"time"
)
type WrappedWriter struct {
http.ResponseWriter
StatusCode int
}
func (w *WrappedWriter) WriteHeader(code int) {
w.StatusCode = code
w.ResponseWriter.WriteHeader(code)
}
func Log(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
wrapped := &WrappedWriter{
ResponseWriter: w,
StatusCode: http.StatusOK,
}
next.ServeHTTP(wrapped, r)
slog.Info("request",
"remoteAddr", r.RemoteAddr,
"status", wrapped.StatusCode,
"method", r.Method,
"path", r.URL.Path,
"duration", time.Since(start).String())
})
}