Skip to content

Commit 091adb0

Browse files
authored
fix: do not import test packages in logp package (#309)
* fix: do not import test packages in logp package logp is importing testing and zaptest which is linked in the binary of apps using this library move NewTestingLogger to a separate package and use WrapCore zap option to replace the logger core with the testing logger * lint: rename
1 parent d752da9 commit 091adb0

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

logp/core.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ func DevelopmentSetup(options ...Option) error {
405405
// TestingSetup configures logging by calling DevelopmentSetup if and only if
406406
// verbose testing is enabled (as in 'go test -v').
407407
//
408-
// Deprecated: Prefer using localized loggers. Use logp.NewTestingLogger.
408+
// Deprecated: Prefer using localized loggers. Use logptest.NewTestingLogger.
409409
func TestingSetup(options ...Option) error {
410410
// Use the flag to avoid a dependency on the testing package.
411411
f := flag.Lookup("test.v")

logp/logger.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,14 @@ import (
2121
"bytes"
2222
"fmt"
2323
"io"
24-
"testing"
2524

2625
"go.elastic.co/ecszap"
2726
"go.uber.org/zap"
2827
"go.uber.org/zap/zapcore"
29-
"go.uber.org/zap/zaptest"
3028
)
3129

3230
// LogOption configures a Logger.
3331
type LogOption = zap.Option
34-
type LogTestOption = zaptest.LoggerOption
3532

3633
// Logger logs messages to the configured output.
3734
type Logger struct {
@@ -76,13 +73,6 @@ func NewDevelopmentLogger(selector string, options ...LogOption) (*Logger, error
7673
return &Logger{log, log.Sugar()}, nil
7774
}
7875

79-
// NewTestingLogger returns a testing suitable logp.Logger.
80-
func NewTestingLogger(t *testing.T, selector string, options ...LogTestOption) *Logger {
81-
log := zaptest.NewLogger(t, options...)
82-
log = log.Named(selector)
83-
return &Logger{log, log.Sugar()}
84-
}
85-
8676
// NewInMemory returns a new in-memory logger along with the buffer to which it
8777
// logs. It's goroutine safe, but operating directly on the returned buffer is not.
8878
// This logger is primary intended for short and simple use-cases such as printing

logp/logptest/logptest.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package logptest
19+
20+
import (
21+
"testing"
22+
23+
"github.com/elastic/elastic-agent-libs/logp"
24+
"go.uber.org/zap"
25+
"go.uber.org/zap/zapcore"
26+
"go.uber.org/zap/zaptest"
27+
)
28+
29+
// NewTestingLogger returns a testing suitable logp.Logger.
30+
func NewTestingLogger(t testing.TB, selector string, options ...logp.LogOption) *logp.Logger {
31+
log := zaptest.NewLogger(t)
32+
log = log.Named(selector)
33+
options = append(options, zap.WrapCore(func(zapcore.Core) zapcore.Core {
34+
return log.Core()
35+
}))
36+
logger, err := logp.NewDevelopmentLogger(selector, options...)
37+
if err != nil {
38+
t.Fatal(err)
39+
}
40+
return logger
41+
}

0 commit comments

Comments
 (0)