Skip to content

Commit a7b80d5

Browse files
feat(log): add NewTestLogger{Info,Error} constructors (#15604)
1 parent ed2eb6f commit a7b80d5

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

log/testing.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,37 @@ type TestingT zerolog.TestingLog
88

99
// NewTestLogger returns a logger that calls t.Log to write entries.
1010
//
11+
// The returned logger emits messages at any level.
12+
// For active debugging of a test with verbose logs,
13+
// the [NewTestLoggerInfo] and [NewTestLoggerError] functions
14+
// only emit messages at or above the corresponding log levels.
15+
//
1116
// If the logs may help debug a test failure,
1217
// you may want to use NewTestLogger(t) in your test.
1318
// Otherwise, use NewNopLogger().
1419
func NewTestLogger(t TestingT) Logger {
20+
return newTestLogger(t, zerolog.DebugLevel)
21+
}
22+
23+
// NewTestLoggerInfo returns a test logger that filters out messages
24+
// below info level.
25+
//
26+
// This is primarily helpful during active debugging of a test
27+
// with verbose logs.
28+
func NewTestLoggerInfo(t TestingT) Logger {
29+
return newTestLogger(t, zerolog.InfoLevel)
30+
}
31+
32+
// NewTestLoggerError returns a test logger that filters out messages
33+
// below Error level.
34+
//
35+
// This is primarily helpful during active debugging of a test
36+
// with verbose logs.
37+
func NewTestLoggerError(t TestingT) Logger {
38+
return newTestLogger(t, zerolog.ErrorLevel)
39+
}
40+
41+
func newTestLogger(t TestingT, lvl zerolog.Level) Logger {
1542
cw := zerolog.NewConsoleWriter()
1643
cw.Out = zerolog.TestWriter{
1744
T: t,
@@ -22,5 +49,5 @@ func NewTestLogger(t TestingT) Logger {
2249
// but Frame=7 prints correct source locations.
2350
Frame: 7,
2451
}
25-
return NewCustomLogger(zerolog.New(cw))
52+
return NewCustomLogger(zerolog.New(cw).Level(lvl))
2653
}

0 commit comments

Comments
 (0)