Files
vikingowl d23ddba1ca fix: skip logging for health and readiness probe endpoints
Suppress log output for /healthz and /readyz to reduce noise from
Kubernetes liveness/readiness probes hitting every 10 seconds.
2026-02-22 20:26:58 +01:00

59 lines
1.1 KiB
Go

package middleware
import (
"log/slog"
"time"
"github.com/gin-gonic/gin"
)
func Logging() gin.HandlerFunc {
return func(c *gin.Context) {
path := c.Request.URL.Path
// Skip logging for health/readiness probes to reduce noise.
if path == "/healthz" || path == "/readyz" {
c.Next()
return
}
start := time.Now()
query := c.Request.URL.RawQuery
c.Next()
latency := time.Since(start)
status := c.Writer.Status()
attrs := []slog.Attr{
slog.String("method", c.Request.Method),
slog.String("path", path),
slog.Int("status", status),
slog.Duration("latency", latency),
slog.String("ip", c.ClientIP()),
}
if query != "" {
attrs = append(attrs, slog.String("query", query))
}
if reqID, exists := c.Get("request_id"); exists {
attrs = append(attrs, slog.String("request_id", reqID.(string)))
}
if len(c.Errors) > 0 {
attrs = append(attrs, slog.String("errors", c.Errors.String()))
}
msg := "request"
level := slog.LevelInfo
if status >= 500 {
level = slog.LevelError
} else if status >= 400 {
level = slog.LevelWarn
}
slog.LogAttrs(c.Request.Context(), level, msg, attrs...)
}
}