cmd/derper, types/logger: move log filter to shared package

So we can use it in trunkd to quiet down the logs there.

Updates #5563

Signed-off-by: Andrew Dunham <andrew@du.nham.ca>
Change-Id: Ie3177dc33f5ad103db832aab5a3e0e4f128f973f
This commit is contained in:
Andrew Dunham
2024-02-15 17:16:06 -05:00
parent 3047b6274c
commit 34176432d6
3 changed files with 45 additions and 21 deletions
+23
View File
@@ -20,6 +20,7 @@ import (
"context"
"go4.org/mem"
"tailscale.com/envknob"
"tailscale.com/util/ctxkey"
)
@@ -393,3 +394,25 @@ func TestLogger(tb TBLogger) Logf {
tb.Logf(" ... "+format, args...)
}
}
// HTTPServerLogFilter is an io.Writer that can be used as the
// net/http.Server.ErrorLog logger, and will filter out noisy, low-signal
// messages that clutter up logs.
type HTTPServerLogFilter struct {
Inner Logf
}
func (lf HTTPServerLogFilter) Write(p []byte) (int, error) {
b := mem.B(p)
if mem.HasSuffix(b, mem.S(": EOF\n")) ||
mem.HasSuffix(b, mem.S(": i/o timeout\n")) ||
mem.HasSuffix(b, mem.S(": read: connection reset by peer\n")) ||
mem.HasSuffix(b, mem.S(": remote error: tls: bad certificate\n")) ||
mem.HasSuffix(b, mem.S(": tls: first record does not look like a TLS handshake\n")) {
// Skip this log message, but say that we processed it
return len(p), nil
}
lf.Inner("%s", p)
return len(p), nil
}
+20
View File
@@ -258,3 +258,23 @@ func TestAsJSON(t *testing.T) {
t.Errorf("allocs = %v; want max 2", n)
}
}
func TestHTTPServerLogFilter(t *testing.T) {
var buf bytes.Buffer
logf := func(format string, args ...any) {
t.Logf("[logf] "+format, args...)
fmt.Fprintf(&buf, format, args...)
}
lf := HTTPServerLogFilter{logf}
quietLogger := log.New(lf, "", 0)
quietLogger.Printf("foo bar")
quietLogger.Printf("http: TLS handshake error from %s:%d: EOF", "1.2.3.4", 9999)
quietLogger.Printf("baz")
const want = "foo bar\nbaz\n"
if s := buf.String(); s != want {
t.Errorf("got buf=%q, want %q", s, want)
}
}