From a7b80d5d8f22047ec21c1750ce4317d3a70ee646 Mon Sep 17 00:00:00 2001 From: Mark Rushakoff Date: Wed, 29 Mar 2023 14:20:49 -0400 Subject: [PATCH] feat(log): add NewTestLogger{Info,Error} constructors (#15604) --- log/testing.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/log/testing.go b/log/testing.go index 7f231ac21318..f31f6e5182f4 100644 --- a/log/testing.go +++ b/log/testing.go @@ -8,10 +8,37 @@ type TestingT zerolog.TestingLog // NewTestLogger returns a logger that calls t.Log to write entries. // +// The returned logger emits messages at any level. +// For active debugging of a test with verbose logs, +// the [NewTestLoggerInfo] and [NewTestLoggerError] functions +// only emit messages at or above the corresponding log levels. +// // If the logs may help debug a test failure, // you may want to use NewTestLogger(t) in your test. // Otherwise, use NewNopLogger(). func NewTestLogger(t TestingT) Logger { + return newTestLogger(t, zerolog.DebugLevel) +} + +// NewTestLoggerInfo returns a test logger that filters out messages +// below info level. +// +// This is primarily helpful during active debugging of a test +// with verbose logs. +func NewTestLoggerInfo(t TestingT) Logger { + return newTestLogger(t, zerolog.InfoLevel) +} + +// NewTestLoggerError returns a test logger that filters out messages +// below Error level. +// +// This is primarily helpful during active debugging of a test +// with verbose logs. +func NewTestLoggerError(t TestingT) Logger { + return newTestLogger(t, zerolog.ErrorLevel) +} + +func newTestLogger(t TestingT, lvl zerolog.Level) Logger { cw := zerolog.NewConsoleWriter() cw.Out = zerolog.TestWriter{ T: t, @@ -22,5 +49,5 @@ func NewTestLogger(t TestingT) Logger { // but Frame=7 prints correct source locations. Frame: 7, } - return NewCustomLogger(zerolog.New(cw)) + return NewCustomLogger(zerolog.New(cw).Level(lvl)) }