Trim comments to the non-obvious why
Drop narration, history and restated-code comments across the repo, keep short ones for security boundaries, upstream quirks and ordering constraints, and record the convention in CLAUDE.md. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -7,42 +7,21 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// This file replaces gin.Logger with a formatter tuned for one reader: the
|
||||
// site owner scanning the log for "who is using my site". Two goals:
|
||||
//
|
||||
// 1. Quietness. The log is flooded by traffic that is not a human visit:
|
||||
// Caddy forward_auth subrequests (/auth/validate-admin, re-run for every
|
||||
// admin page hit), the front end's own polling (`/auth/refresh_token`,
|
||||
// home-data refreshes), and Vite dev asset fetches. Each is collapsed to
|
||||
// a single terse line, or dropped entirely when repeated in a short
|
||||
// window — a 200 on a poll says nothing worth a line each time.
|
||||
//
|
||||
// 2. Salience. A request that matters (an external IP, an error status, a
|
||||
// slow response) gets a line that stands out and carries the pieces that
|
||||
// answer "who": client IP, path, status, latency.
|
||||
// TrafficLogFormatter replaces gin.Logger: heartbeat and forward_auth traffic
|
||||
// is dropped or terse, while external IPs, errors and slow requests stand out.
|
||||
|
||||
// logQuietDrop lists paths whose 2xx responses are dropped outright. These
|
||||
// are all heartbeat-style endpoints: identical every few seconds, no signal.
|
||||
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
|
||||
}
|
||||
|
||||
// logQuietDropPrefix is the permission gate's auth_request subrequest
|
||||
// (/auth/validate/<key>), dropped on 2xx for the same reason as
|
||||
// /auth/validate-admin above: it repeats on every request to a gated page.
|
||||
// The permission gate's subrequests, as for /auth/validate-admin.
|
||||
const logQuietDropPrefix = "/auth/validate/"
|
||||
|
||||
// TrafficLogFormatter renders gin's log line. Signed to gin's LogFormatter
|
||||
// signature so it plugs into gin.LoggerWithFormatter.
|
||||
func TrafficLogFormatter(params gin.LogFormatterParams) string {
|
||||
// gin hands the full URL (path + query) in Path; Query is split off
|
||||
// params.ErrorMessage... not needed — just use Path which carries both.
|
||||
path := params.Path
|
||||
|
||||
// Latency is human-scaled rather than raw nanoseconds: "1.2ms" scans
|
||||
// faster than "1230689ns" when you are skimming.
|
||||
latency := params.Latency
|
||||
unit := "ns"
|
||||
switch {
|
||||
@@ -56,8 +35,6 @@ func TrafficLogFormatter(params gin.LogFormatterParams) string {
|
||||
unit, latency = "µs", latency/time.Microsecond
|
||||
}
|
||||
|
||||
// Errors (>=500) are marked so they survive even a fast scroll; the
|
||||
// literal marker also makes them greppable in a rotated log.
|
||||
marker := ""
|
||||
if params.StatusCode >= 500 {
|
||||
marker = " !!! SERVER ERROR"
|
||||
@@ -65,9 +42,7 @@ func TrafficLogFormatter(params gin.LogFormatterParams) string {
|
||||
marker = " !!"
|
||||
}
|
||||
|
||||
// All-quiet: drop heartbeat endpoints that succeeded. A 4xx/5xx on them
|
||||
// is still interesting (an expired session raging against refresh, a
|
||||
// broken admin gate) and is kept.
|
||||
// Failures on these are still worth seeing.
|
||||
if (logQuietDrop[path] || startsWith(path, logQuietDropPrefix)) && params.StatusCode < 400 {
|
||||
return ""
|
||||
}
|
||||
@@ -75,9 +50,6 @@ func TrafficLogFormatter(params gin.LogFormatterParams) string {
|
||||
line := fmt.Sprintf("%s %s %d %s%s", params.Method, path, params.StatusCode,
|
||||
fmt.Sprintf("%.0f%s", float64(latency), unit), marker)
|
||||
|
||||
// Visitor traffic gets the IP appended — the "who". Infrastructure
|
||||
// traffic (the edge's own feet on the internal subnet) already has its IP
|
||||
// implied and is left lean.
|
||||
if params.StatusCode >= 400 || logIsInterestingClient(params.ClientIP) {
|
||||
line += " " + params.ClientIP
|
||||
}
|
||||
@@ -85,11 +57,8 @@ func TrafficLogFormatter(params gin.LogFormatterParams) string {
|
||||
return line + "\n"
|
||||
}
|
||||
|
||||
// logIsInterestingClient reports whether a client IP is likely an actual
|
||||
// visitor rather than infrastructure. Caddy proxies everything, so the IPs
|
||||
// that appear are either forwarded-from-humans or the ingress itself.
|
||||
func logIsInterestingClient(ip string) bool {
|
||||
// Docker's bridge network: the edge's own feet. Not a visitor.
|
||||
// The Docker bridge: infrastructure, not a visitor.
|
||||
return !startsWith(ip, "172.28.")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user