Skip to content

Commit 5cb60ba

Browse files
committed
Address review comments
1 parent 3bd26ab commit 5cb60ba

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

logp/configure/logging_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ package configure
2020
import (
2121
"testing"
2222

23+
"github.com/stretchr/testify/require"
24+
2325
"github.com/elastic/elastic-agent-libs/config"
2426
"github.com/elastic/elastic-agent-libs/logp"
25-
"github.com/stretchr/testify/require"
2627
)
2728

2829
func TestLoggerOutputEnvironment(t *testing.T) {

logp/logptest/logger.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import (
4545
// when searching for logs
4646
type Logger struct {
4747
*logp.Logger
48-
t *testing.T
4948
logFile *os.File
5049
offset int64
5150
}
@@ -68,7 +67,6 @@ func NewFileLogger(t *testing.T) *Logger {
6867
core := zapcore.NewCore(encoder, zapcore.AddSync(f), zap.DebugLevel)
6968

7069
tl := &Logger{
71-
t: t,
7270
logFile: f,
7371
}
7472

@@ -115,12 +113,12 @@ func NewFileLogger(t *testing.T) *Logger {
115113
// subsequent call to WaitLogsContains will only check logs not yet evaluated.
116114
// msgAndArgs should be a format string and arguments that will be printed
117115
// if the logs are not found, providing additional context for debugging.
118-
func (l *Logger) WaitLogsContains(s string, timeout time.Duration, msgAndArgs ...any) {
119-
l.t.Helper()
116+
func (l *Logger) WaitLogsContains(t *testing.T, s string, timeout time.Duration, msgAndArgs ...any) {
117+
t.Helper()
120118
require.EventuallyWithT(
121-
l.t,
119+
t,
122120
func(c *assert.CollectT) {
123-
found, err := l.logContains(s)
121+
found, err := l.LogContains(s)
124122
if err != nil {
125123
c.Errorf("cannot check the log file: %s", err)
126124
return
@@ -135,25 +133,25 @@ func (l *Logger) WaitLogsContains(s string, timeout time.Duration, msgAndArgs ..
135133
msgAndArgs...)
136134
}
137135

138-
// logContains searches for str in the log file keeping track of the offset.
139-
// If there is any issue reading the log file, then t.Fatalf is called,
140-
// if str is not present in the logs, t.Fatalf is called.
141-
func (l *Logger) LogContains(str string) {
142-
l.t.Helper()
143-
found, err := l.logContains(str)
136+
// RequireLogContains searches for str in the log file keeping track of the
137+
// offset. If there is any issue reading the log file, then t.Fatalf is called,
138+
// if str is not present in the logs, t.Errorf is called.
139+
func (l *Logger) RequireLogContains(t *testing.T, str string) {
140+
t.Helper()
141+
found, err := l.LogContains(str)
144142
if err != nil {
145-
l.t.Fatalf("cannot read log file: %s", err)
143+
t.Fatalf("cannot read log file: %s", err)
146144
}
147145

148146
if !found {
149-
l.t.Fatalf("'%s' not found in logs", str)
147+
t.Errorf("'%s' not found in logs", str)
150148
}
151149
}
152150

153151
// logContains searches for str in the log file keeping track of the offset.
154152
// It returns true if str is found in the logs. If there are any errors,
155153
// it returns false and the error
156-
func (l *Logger) logContains(str string) (bool, error) {
154+
func (l *Logger) LogContains(str string) (bool, error) {
157155
// Open the file again so we can seek and not interfere with
158156
// the logger writing to it.
159157
f, err := os.Open(l.logFile.Name())

logp/logptest/logptest.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ import (
2929
)
3030

3131
// NewTestingLogger returns a testing suitable logp.Logger.
32+
// Deprecated: Use [NewFileLogger] instead, it provides a [logp.Logger],
33+
// methods to search in the logs and will keep a file with all the logs if
34+
// the test fails.
3235
func NewTestingLogger(t testing.TB, selector string, options ...logp.LogOption) *logp.Logger {
3336
log := zaptest.NewLogger(t, zaptest.WrapOptions(options...))
3437
log = log.Named(selector)

0 commit comments

Comments
 (0)