test(diary): config, channels, rotation, redaction and slogtest

- config rules name their key; env strings overlay defaults; D-08 defaults
- component floors, verbosity 1-3, quiet on the console only
- daily rotation, retention, 0640 files, unwritable dir, lazy open, path confinement
- redaction on every driver, console control-character stripping and prefix
- loggers before Setup, Reset, HideOnTTY, 100 concurrent writers
- testing/slogtest for the console handler and the JSON pipeline
- sunscreen InstallDefault still installs a redacting logfmt handler
This commit is contained in:
Jakub Zych
2026-10-21 19:09:28 +02:00
parent 9b2fa6c324
commit 24f4643283
6 changed files with 1364 additions and 0 deletions

View File

@@ -142,3 +142,42 @@ func TestInstallDefault(t *testing.T) {
t.Fatalf("default logger output:\n%s", out)
}
}
// TestInstallDefaultUnchanged: InstallDefault, the compatibility entry
// point that generated applications no longer call, still installs a
// redacting logfmt TextHandler on the given writer, and each call replaces
// the previous default instead of wrapping it.
func TestInstallDefaultUnchanged(t *testing.T) {
prev := slog.Default()
prevFlags, prevWriter := log.Flags(), log.Writer()
t.Cleanup(func() {
slog.SetDefault(prev)
log.SetFlags(prevFlags)
log.SetOutput(prevWriter)
})
var first, second bytes.Buffer
InstallDefault(&first)
InstallDefault(&second)
slog.Debug("below the default level", "token", nested)
slog.Info("payment for Bearer "+bearer, "api_key", apiKey, slog.Group("g", "Password", passwd))
if first.Len() != 0 {
t.Fatalf("the replaced default still receives records:\n%s", first.String())
}
out := second.String()
if strings.Count(out, "\n") != 1 {
t.Fatalf("want exactly one line (info level, debug dropped):\n%s", out)
}
for _, want := range []string{"time=", " level=INFO ", `msg="payment for Bearer [REDACTED]"`, "api_key=[REDACTED]", "g.Password=[REDACTED]"} {
if !strings.Contains(out, want) {
t.Errorf("default output lacks %q:\n%s", want, out)
}
}
for _, secret := range []string{bearer, apiKey, passwd, nested} {
if strings.Contains(out, secret) {
t.Errorf("default output leaks %q:\n%s", secret, out)
}
}
if strings.HasPrefix(strings.TrimSpace(out), "{") {
t.Errorf("default output is JSON, want logfmt:\n%s", out)
}
}