Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add zaptest logger option to wrap zap.Option's #610

Merged
merged 2 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions zaptest/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ type LoggerOption interface {
}

type loggerOptions struct {
Level zapcore.LevelEnabler
Level zapcore.LevelEnabler
AddCaller bool
}

type loggerOptionFunc func(*loggerOptions)
Expand All @@ -50,6 +51,13 @@ func Level(enab zapcore.LevelEnabler) LoggerOption {
})
}

// AddCaller ensures caller is logged by a test Logger built by NewLogger.
func AddCaller() LoggerOption {
return loggerOptionFunc(func(opts *loggerOptions) {
opts.AddCaller = true
})
}

// NewLogger builds a new Logger that logs all messages to the given
// testing.TB.
//
Expand All @@ -59,9 +67,14 @@ func Level(enab zapcore.LevelEnabler) LoggerOption {
// if a test fails or if you ran go test -v.
//
// The returned logger defaults to logging debug level messages and above.
// This may be changd by passing a zaptest.Level during construction.
// This may be changed by passing a zaptest.Level during construction.
//
// logger := zaptest.NewLogger(t, zaptest.Level(zap.WarnLevel))
//
// The returned logger doesn't log caller.
// This may be changed by passing a zaptest.AddCaller during construction.
//
// logger := zaptest.NewLogger(t, zaptest.AddCaller())
func NewLogger(t TestingT, opts ...LoggerOption) *zap.Logger {
cfg := loggerOptions{
Level: zapcore.DebugLevel,
Expand All @@ -71,16 +84,22 @@ func NewLogger(t TestingT, opts ...LoggerOption) *zap.Logger {
}

writer := newTestingWriter(t)
zapOptions := []zap.Option{
// Send zap errors to the same writer and mark the test as failed if
// that happens.
zap.ErrorOutput(writer.WithMarkFailed(true)),
}
if cfg.AddCaller {
zapOptions = append(zapOptions, zap.AddCaller())
}

return zap.New(
zapcore.NewCore(
zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
writer,
cfg.Level,
),

// Send zap errors to the same writer and mark the test as failed if
// that happens.
zap.ErrorOutput(writer.WithMarkFailed(true)),
zapOptions...,
)
}

Expand Down
24 changes: 24 additions & 0 deletions zaptest/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,30 @@ func TestTestLoggerSupportsLevels(t *testing.T) {
)
}

func TestTestLoggerSupportsAddCaller(t *testing.T) {
ts := newTestLogSpy(t)
defer ts.AssertPassed()

log := NewLogger(ts, AddCaller())

log.Info("received work order")
log.Debug("starting work")
log.Warn("work may fail")
log.Error("work failed", zap.Error(errors.New("great sadness")))

assert.Panics(t, func() {
log.Panic("failed to do work")
}, "log.Panic should panic")

ts.AssertMessages(
"INFO zaptest/logger_test.go:89 received work order",
"DEBUG zaptest/logger_test.go:90 starting work",
"WARN zaptest/logger_test.go:91 work may fail",
`ERROR zaptest/logger_test.go:92 work failed {"error": "great sadness"}`,
"PANIC zaptest/logger_test.go:95 failed to do work",
)
}

func TestTestingWriter(t *testing.T) {
ts := newTestLogSpy(t)
w := newTestingWriter(ts)
Expand Down