chore: update TBLogHandler

This commit is contained in:
Jonathan Oppenheimer 2026-03-02 11:39:59 -05:00
parent 312fa38051
commit a033f727d5
No known key found for this signature in database
GPG key ID: E4CEF9010EB8B740
2 changed files with 44 additions and 23 deletions

View file

@ -27,10 +27,10 @@ import (
"github.com/ava-labs/libevm/log" "github.com/ava-labs/libevm/log"
) )
// NewTBLogHandler constructs a [slog.Handler] that propagates logs to [testing.TB]. // NewTBLogHandler constructs a [slog.Handler] that propagates all logs to
// Logs at [log.LevelWarn] or above go to [testing.TB.Errorf], except // [testing.TB]. Logs at or above `level` go to [testing.TB.Errorf], except
// [log.LevelCrit] which goes to [testing.TB.Fatalf]. All other logs go to // [log.LevelCrit] which goes to [testing.TB.Fatalf]. All other logs go to
// [testing.TB.Logf]. The level parameter controls which logs are enabled. // [testing.TB.Logf].
// //
//nolint:thelper // The outputs include the logging site while the TB site is most useful if here //nolint:thelper // The outputs include the logging site while the TB site is most useful if here
func NewTBLogHandler(tb testing.TB, level slog.Level) slog.Handler { func NewTBLogHandler(tb testing.TB, level slog.Level) slog.Handler {
@ -46,8 +46,8 @@ type tbHandler struct {
attrs []slog.Attr attrs []slog.Attr
} }
func (h *tbHandler) Enabled(_ context.Context, level slog.Level) bool { func (h *tbHandler) Enabled(_ context.Context, _ slog.Level) bool {
return level >= min(h.level, slog.LevelWarn) return true
} }
func (h *tbHandler) Handle(_ context.Context, rec slog.Record) error { func (h *tbHandler) Handle(_ context.Context, rec slog.Record) error {
@ -55,7 +55,7 @@ func (h *tbHandler) Handle(_ context.Context, rec slog.Record) error {
switch { switch {
case rec.Level >= log.LevelCrit: case rec.Level >= log.LevelCrit:
to = h.tb.Fatalf to = h.tb.Fatalf
case rec.Level >= log.LevelWarn: case rec.Level >= h.level:
to = h.tb.Errorf to = h.tb.Errorf
} }

View file

@ -41,25 +41,46 @@ func (r *tbRecorder) Errorf(format string, a ...any) {
} }
func TestTBLogHandler(t *testing.T) { func TestTBLogHandler(t *testing.T) {
got := &tbRecorder{} tests := []struct {
l := log.NewLogger(NewTBLogHandler(got, slog.LevelDebug)) name string
level slog.Level
wantLog []string
wantErr []string
}{
{
name: "warn_level",
level: slog.LevelWarn,
wantLog: []string{"Cockroach", "Hello"},
wantErr: []string{"Smoke", "Fire"},
},
{
name: "error_level",
level: slog.LevelError,
wantLog: []string{"Cockroach", "Hello", "Smoke"},
wantErr: []string{"Fire"},
},
}
l.Debug("Cockroach") // Logf for _, tt := range tests {
l.Info("Hello", "who", "world") // Logf t.Run(tt.name, func(t *testing.T) {
l.Warn("Smoke") // Errorf got := &tbRecorder{}
l.Error("Fire") // Errorf l := log.NewLogger(NewTBLogHandler(got, tt.level))
// Crit will call os.Exit(1) so we don't test it.
require.Len(t, got.logged, 2, "Logf() calls") l.Debug("Cockroach")
require.Len(t, got.errored, 2, "Errorf() calls") l.Info("Hello", "who", "world")
l.Warn("Smoke")
l.Error("Fire")
// Crit will call os.Exit(1) so we don't test it.
// Check simplest elements without being brittle about exact formatting require.Len(t, got.logged, len(tt.wantLog), "Logf() calls")
// See https://testing.googleblog.com/2015/01/testing-on-toilet-change-detector-tests.html. require.Len(t, got.errored, len(tt.wantErr), "Errorf() calls")
assert.Contains(t, got.logged[0], "Cockroach")
assert.Contains(t, got.logged[1], "Hello")
assert.Contains(t, got.logged[1], "who")
assert.Contains(t, got.logged[1], "world")
assert.Contains(t, got.errored[0], "Smoke") for i, want := range tt.wantLog {
assert.Contains(t, got.errored[1], "Fire") assert.Contains(t, got.logged[i], want, "Logf()[%d]", i)
}
for i, want := range tt.wantErr {
assert.Contains(t, got.errored[i], want, "Errorf()[%d]", i)
}
})
}
} }