diff --git a/zaptest/logger.go b/zaptest/logger.go index 80ace98ea..1e2451c26 100644 --- a/zaptest/logger.go +++ b/zaptest/logger.go @@ -33,7 +33,8 @@ type LoggerOption interface { } type loggerOptions struct { - Level zapcore.LevelEnabler + Level zapcore.LevelEnabler + zapOptions []zap.Option } type loggerOptionFunc func(*loggerOptions) @@ -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. // @@ -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, @@ -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..., ) } diff --git a/zaptest/logger_test.go b/zaptest/logger_test.go index b69aa28c8..576f6828c 100644 --- a/zaptest/logger_test.go +++ b/zaptest/logger_test.go @@ -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)