Skip to content

Commit 34ff24f

Browse files
committed
Prevent logptest.NewTestingLogger from panics.
Because NewTestingLogger used testing.T as the logger output, if the logger is used after the test has ended, it will panic. This behaviour has been seen a few times and causes flakiness in our CI. To mitigate this, NewTestingLogger now just calls NewFileLogger that uses a file as the logger output, thus being safe for use even after the test has ended.
1 parent 03ca452 commit 34ff24f

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

logp/logptest/logptest.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,19 @@ import (
2222

2323
"go.uber.org/zap"
2424
"go.uber.org/zap/zapcore"
25-
"go.uber.org/zap/zaptest"
2625
"go.uber.org/zap/zaptest/observer"
2726

2827
"github.com/elastic/elastic-agent-libs/logp"
2928
)
3029

31-
// NewTestingLogger returns a testing suitable logp.Logger that uses the
32-
// [testing.T] as the logger output.
30+
// NewTestingLogger Just calls [NewFileLogger], the log if is placed in the
31+
// the folder returned by [os.TempDir].
32+
//
33+
// DEPRECATED: The logger returned by [NewTestingLogger] can panic if it is
34+
// used after the test has ended. Use [NewFileLogger] instead.
3335
func NewTestingLogger(t testing.TB, selector string, options ...logp.LogOption) *logp.Logger {
34-
log := zaptest.NewLogger(t, zaptest.WrapOptions(options...))
35-
log = log.Named(selector)
36-
37-
logger, err := logp.NewZapLogger(log)
38-
if err != nil {
39-
t.Fatal(err)
40-
}
41-
return logger
36+
l := NewFileLogger(t, "")
37+
return l.Logger
4238
}
4339

4440
// NewTestingLoggerWithObserver returns a testing suitable logp.Logger that uses the

0 commit comments

Comments
 (0)