// Package testlog silences slog in test binaries. // // The hold, PDS and appview packages log at INFO while they boot, subscribe, // and tear down, and every test that starts one of them produces dozens of // lines. `go test` only prints a package's output when it fails, so the noise // lands exactly where the failure message is, and buries it. // // Call Quiet from a package's TestMain. Logs come back with `go test -v` or // ATCR_TEST_LOGS=1, so a failing run can be re-run with them on. Note that // `go test -json` implies -v, so tooling that streams JSON sees the logs too. package testlog import ( "flag" "io" "log/slog" "os" "testing" ) // Quiet replaces the default slog logger with one that discards everything, // unless the test binary runs verbose (-v) or ATCR_TEST_LOGS is set. It parses // the test flags if that has not happened yet, which TestMain otherwise leaves // to m.Run. func Quiet() { if !flag.Parsed() { flag.Parse() } if testing.Verbose() || os.Getenv("ATCR_TEST_LOGS") != "" { return } slog.SetDefault(slog.New(slog.NewTextHandler(io.Discard, nil))) }