Reduce API log noise
This commit is contained in:
+28
-2
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user