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 all commits
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
28 changes: 22 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
zapOptions []zap.Option
}

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

// WrapOptions adds zap.Option's to a test Logger built by NewLogger.
func WrapOptions(zapOpts ...zap.Option) LoggerOption {
return loggerOptionFunc(func(opts *loggerOptions) {
opts.zapOptions = zapOpts
})
}

// NewLogger builds a new Logger that logs all messages to the given
// testing.TB.
//
Expand All @@ -59,9 +67,13 @@ 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))
//
// You may also pass zap.Option's to customize test logger.
//
// logger := zaptest.NewLogger(t, zaptest.WrapOptions(zap.AddCaller()))
func NewLogger(t TestingT, opts ...LoggerOption) *zap.Logger {
cfg := loggerOptions{
Level: zapcore.DebugLevel,
Expand All @@ -71,16 +83,20 @@ 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)),
}
zapOptions = append(zapOptions, cfg.zapOptions...)

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 TestTestLoggerSupportsWrappedZapOptions(t *testing.T) {
ts := newTestLogSpy(t)
defer ts.AssertPassed()

log := NewLogger(ts, WrapOptions(zap.AddCaller(), zap.Fields(zap.String("k1", "v1"))))

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 {"k1": "v1"}`,
`DEBUG zaptest/logger_test.go:90 starting work {"k1": "v1"}`,
`WARN zaptest/logger_test.go:91 work may fail {"k1": "v1"}`,
`ERROR zaptest/logger_test.go:92 work failed {"k1": "v1", "error": "great sadness"}`,
`PANIC zaptest/logger_test.go:95 failed to do work {"k1": "v1"}`,
)
}

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