From f73bd51fb9c5cb88e48fb3f99ac4f33155b4ccf4 Mon Sep 17 00:00:00 2001 From: Iaroslav Ciupin Date: Tue, 17 Jul 2018 17:03:41 +0300 Subject: [PATCH 1/2] Add zaptest logger option to add caller --- zaptest/logger.go | 31 +++++++++++++++++++++++++------ zaptest/logger_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/zaptest/logger.go b/zaptest/logger.go index 80ace98ea..944761374 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 + AddCaller bool } type loggerOptionFunc func(*loggerOptions) @@ -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. // @@ -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, @@ -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..., ) } diff --git a/zaptest/logger_test.go b/zaptest/logger_test.go index b69aa28c8..2fd2381a1 100644 --- a/zaptest/logger_test.go +++ b/zaptest/logger_test.go @@ -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) From 3e7caedf18ff1e2f13364ba23f0ce17aed64ee6e Mon Sep 17 00:00:00 2001 From: Iaroslav Ciupin Date: Wed, 8 Aug 2018 14:13:45 +0300 Subject: [PATCH 2/2] Add zaptest logger option for wrapping zap.Option's --- zaptest/logger.go | 19 ++++++++----------- zaptest/logger_test.go | 14 +++++++------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/zaptest/logger.go b/zaptest/logger.go index 944761374..1e2451c26 100644 --- a/zaptest/logger.go +++ b/zaptest/logger.go @@ -33,8 +33,8 @@ type LoggerOption interface { } type loggerOptions struct { - Level zapcore.LevelEnabler - AddCaller bool + Level zapcore.LevelEnabler + zapOptions []zap.Option } type loggerOptionFunc func(*loggerOptions) @@ -51,10 +51,10 @@ func Level(enab zapcore.LevelEnabler) LoggerOption { }) } -// AddCaller ensures caller is logged by a test Logger built by NewLogger. -func AddCaller() 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.AddCaller = true + opts.zapOptions = zapOpts }) } @@ -71,10 +71,9 @@ func AddCaller() LoggerOption { // // 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. +// You may also pass zap.Option's to customize test logger. // -// logger := zaptest.NewLogger(t, zaptest.AddCaller()) +// logger := zaptest.NewLogger(t, zaptest.WrapOptions(zap.AddCaller())) func NewLogger(t TestingT, opts ...LoggerOption) *zap.Logger { cfg := loggerOptions{ Level: zapcore.DebugLevel, @@ -89,9 +88,7 @@ func NewLogger(t TestingT, opts ...LoggerOption) *zap.Logger { // that happens. zap.ErrorOutput(writer.WithMarkFailed(true)), } - if cfg.AddCaller { - zapOptions = append(zapOptions, zap.AddCaller()) - } + zapOptions = append(zapOptions, cfg.zapOptions...) return zap.New( zapcore.NewCore( diff --git a/zaptest/logger_test.go b/zaptest/logger_test.go index 2fd2381a1..576f6828c 100644 --- a/zaptest/logger_test.go +++ b/zaptest/logger_test.go @@ -80,11 +80,11 @@ func TestTestLoggerSupportsLevels(t *testing.T) { ) } -func TestTestLoggerSupportsAddCaller(t *testing.T) { +func TestTestLoggerSupportsWrappedZapOptions(t *testing.T) { ts := newTestLogSpy(t) defer ts.AssertPassed() - log := NewLogger(ts, AddCaller()) + log := NewLogger(ts, WrapOptions(zap.AddCaller(), zap.Fields(zap.String("k1", "v1")))) log.Info("received work order") log.Debug("starting work") @@ -96,11 +96,11 @@ func TestTestLoggerSupportsAddCaller(t *testing.T) { }, "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", + `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"}`, ) }