package services import ( "fmt" "time" "github.com/gin-gonic/gin" ) // TrafficLogFormatter replaces gin.Logger: heartbeat and forward_auth traffic // is dropped or terse, while external IPs, errors and slow requests stand out. var logQuietDrop = map[string]bool{ "/auth/validate-admin": true, // Caddy forward_auth subrequest "/auth/refresh": true, // front-end token refresh "/auth/check": true, // front-end session check } // The permission gate's subrequests, as for /auth/validate-admin. const logQuietDropPrefix = "/auth/validate/" func TrafficLogFormatter(params gin.LogFormatterParams) string { path := params.Path latency := params.Latency unit := "ns" switch { case latency > time.Minute: unit, latency = "m", latency/time.Minute case latency > time.Second: unit, latency = "s", latency/time.Second case latency > time.Millisecond: unit, latency = "ms", latency/time.Millisecond case latency > time.Microsecond: unit, latency = "µs", latency/time.Microsecond } marker := "" if params.StatusCode >= 500 { marker = " !!! SERVER ERROR" } else if params.StatusCode >= 400 { marker = " !!" } // Failures on these are still worth seeing. if (logQuietDrop[path] || startsWith(path, logQuietDropPrefix)) && params.StatusCode < 400 { return "" } line := fmt.Sprintf("%s %s %d %s%s", params.Method, path, params.StatusCode, fmt.Sprintf("%.0f%s", float64(latency), unit), marker) if params.StatusCode >= 400 || logIsInterestingClient(params.ClientIP) { line += " " + params.ClientIP } return line + "\n" } func logIsInterestingClient(ip string) bool { // The Docker bridge: infrastructure, not a visitor. return !startsWith(ip, "172.28.") } func startsWith(s, prefix string) bool { return len(s) >= len(prefix) && s[:len(prefix)] == prefix }