Skip to content

Commit

Permalink
NewTestLogger => NewLogger; return *zap.Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinav committed Oct 31, 2017
1 parent 9c4600e commit 316a110
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
21 changes: 9 additions & 12 deletions zaptest/test_logger.go → zaptest/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,26 @@ package zaptest
import (
"testing"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

// NewTestLogger builds a new Core that logs all messages to the given
// NewLogger builds a new Logger that logs all messages to the given
// testing.TB.
//
// Use this with a *testing.T or *testing.B to get logs which get printed only
// if a test fails or if you ran go test -v.
//
// logger := zap.New(NewTestLogger(t))
func NewTestLogger(t testing.TB) zapcore.Core {
return NewTestLoggerAt(t, zapcore.DebugLevel)
func NewLogger(t testing.TB) *zap.Logger {
return NewLoggerAt(t, zapcore.DebugLevel)
}

// NewTestLoggerAt builds a new Core that logs messages to the given
// testing.TB if the given LevelEnabler allows it.
// NewLoggerAt builds a new Logger that logs messages to the given testing.TB
// if the given LevelEnabler allows it.
//
// Use this with a *testing.T or *testing.B to get logs which get printed only
// if a test fails or if you ran go test -v.
//
// logger := zap.New(NewTestLoggerAt(t, zap.InfoLevel))
func NewTestLoggerAt(t testing.TB, enab zapcore.LevelEnabler) zapcore.Core {
return zapcore.NewCore(
func NewLoggerAt(t testing.TB, enab zapcore.LevelEnabler) *zap.Logger {
return zap.New(zapcore.NewCore(
zapcore.NewConsoleEncoder(zapcore.EncoderConfig{
// EncoderConfig copied from zap.NewDevelopmentEncoderConfig.
// Can't use it directly because that would cause a cyclic import.
Expand All @@ -63,7 +60,7 @@ func NewTestLoggerAt(t testing.TB, enab zapcore.LevelEnabler) zapcore.Core {
}),
testingWriter{t},
enab,
)
))
}

// testingWriter is a WriteSyncer that writes to the given testing.TB.
Expand Down
4 changes: 2 additions & 2 deletions zaptest/test_logger_test.go → zaptest/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (

func TestTestLoggerIncludesDebug(t *testing.T) {
ts := newTestLogSpy(t)
log := zap.New(NewTestLogger(ts))
log := NewLogger(ts)
log.Debug("calculating")
log.Info("finished calculating", zap.Int("answer", 42))

Expand All @@ -46,7 +46,7 @@ func TestTestLoggerIncludesDebug(t *testing.T) {

func TestTestLoggerAt(t *testing.T) {
ts := newTestLogSpy(t)
log := zap.New(NewTestLoggerAt(ts, zap.WarnLevel))
log := NewLoggerAt(ts, zap.WarnLevel)

log.Info("received work order")
log.Debug("starting work")
Expand Down

0 comments on commit 316a110

Please sign in to comment.