64 lines
1.3 KiB
Go
64 lines
1.3 KiB
Go
package middlewares
|
|
|
|
import (
|
|
"log/slog"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type logMiddleware struct {
|
|
handler http.Handler
|
|
log *slog.Logger
|
|
}
|
|
|
|
type statusRecorder struct {
|
|
http.ResponseWriter
|
|
status int
|
|
}
|
|
|
|
func NewLogger(h http.Handler, log *slog.Logger) *logMiddleware {
|
|
return &logMiddleware{
|
|
handler: h,
|
|
log: log,
|
|
}
|
|
}
|
|
|
|
func (l *logMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
rec := statusRecorder{w, 200}
|
|
l.handler.ServeHTTP(&rec, r)
|
|
if rec.status < 299 {
|
|
l.log.Info(
|
|
"incoming request",
|
|
"status", rec.status,
|
|
"method", r.Method,
|
|
"time_spent_ms", time.Since(start).Milliseconds(),
|
|
"path", r.URL.Path,
|
|
"user_agent", r.Header.Get("User-Agent"),
|
|
)
|
|
} else if rec.status > 299 && rec.status < 399 {
|
|
l.log.Warn(
|
|
"redirecting request",
|
|
"status", rec.status,
|
|
"method", r.Method,
|
|
"time_spent_ms", time.Since(start).Milliseconds(),
|
|
"path", r.URL.Path,
|
|
"user_agent", r.Header.Get("User-Agent"),
|
|
)
|
|
} else {
|
|
l.log.Warn(
|
|
"error on request",
|
|
"status", rec.status,
|
|
"method", r.Method,
|
|
"time_spent_ms", time.Since(start).Milliseconds(),
|
|
"path", r.URL.Path,
|
|
"user_agent", r.Header.Get("User-Agent"),
|
|
)
|
|
}
|
|
}
|
|
|
|
func (rec *statusRecorder) WriteHeader(code int) {
|
|
rec.status = code
|
|
rec.ResponseWriter.WriteHeader(code)
|
|
}
|