diff --git a/libevm/ethtest/logger.go b/libevm/ethtest/logger.go index d78055cf8c..babb40faac 100644 --- a/libevm/ethtest/logger.go +++ b/libevm/ethtest/logger.go @@ -27,10 +27,10 @@ import ( "github.com/ava-labs/libevm/log" ) -// NewTBLogHandler constructs a [slog.Handler] that propagates logs to [testing.TB]. -// Logs at [log.LevelWarn] or above go to [testing.TB.Errorf], except +// NewTBLogHandler constructs a [slog.Handler] that propagates all logs to +// [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 -// [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 func NewTBLogHandler(tb testing.TB, level slog.Level) slog.Handler { @@ -46,8 +46,8 @@ type tbHandler struct { attrs []slog.Attr } -func (h *tbHandler) Enabled(_ context.Context, level slog.Level) bool { - return level >= min(h.level, slog.LevelWarn) +func (h *tbHandler) Enabled(_ context.Context, _ slog.Level) bool { + return true } 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 { case rec.Level >= log.LevelCrit: to = h.tb.Fatalf - case rec.Level >= log.LevelWarn: + case rec.Level >= h.level: to = h.tb.Errorf } diff --git a/libevm/ethtest/logger_test.go b/libevm/ethtest/logger_test.go index dce83a30ab..2ce41d62ad 100644 --- a/libevm/ethtest/logger_test.go +++ b/libevm/ethtest/logger_test.go @@ -41,25 +41,46 @@ func (r *tbRecorder) Errorf(format string, a ...any) { } func TestTBLogHandler(t *testing.T) { - got := &tbRecorder{} - l := log.NewLogger(NewTBLogHandler(got, slog.LevelDebug)) + tests := []struct { + 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 - l.Info("Hello", "who", "world") // Logf - l.Warn("Smoke") // Errorf - l.Error("Fire") // Errorf - // Crit will call os.Exit(1) so we don't test it. + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := &tbRecorder{} + l := log.NewLogger(NewTBLogHandler(got, tt.level)) - require.Len(t, got.logged, 2, "Logf() calls") - require.Len(t, got.errored, 2, "Errorf() calls") + l.Debug("Cockroach") + 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 - // See https://testing.googleblog.com/2015/01/testing-on-toilet-change-detector-tests.html. - 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") + require.Len(t, got.logged, len(tt.wantLog), "Logf() calls") + require.Len(t, got.errored, len(tt.wantErr), "Errorf() calls") - assert.Contains(t, got.errored[0], "Smoke") - assert.Contains(t, got.errored[1], "Fire") + for i, want := range tt.wantLog { + 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) + } + }) + } }