Reduce API log noise

This commit is contained in:
Mikei386
2026-07-10 13:09:56 +02:00
parent 3f1b7cd781
commit 091c5ff175
8 changed files with 46 additions and 11 deletions
+28 -2
View File
@@ -332,7 +332,33 @@ func writeError(w http.ResponseWriter, err error) {
func requestLog(log *slog.Logger, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
started := time.Now()
next.ServeHTTP(w, r)
log.Info("api request", "method", r.Method, "path", r.URL.Path, "durationMs", time.Since(started).Milliseconds())
recorder := &statusRecorder{ResponseWriter: w, status: http.StatusOK}
next.ServeHTTP(recorder, r)
duration := time.Since(started)
if shouldLogRequest(r, recorder.status, duration) {
log.Info("api request", "method", r.Method, "path", r.URL.Path, "status", recorder.status, "durationMs", duration.Milliseconds())
}
})
}
type statusRecorder struct {
http.ResponseWriter
status int
}
func (r *statusRecorder) WriteHeader(status int) {
r.status = status
r.ResponseWriter.WriteHeader(status)
}
func shouldLogRequest(r *http.Request, status int, duration time.Duration) bool {
if status >= 400 || duration >= time.Second {
return true
}
switch r.URL.Path {
case "/v1/runs", "/v1/health", "/v1/logs":
return false
default:
return true
}
}