From b2382d77d379f12350c170a9982492b9ab1ad847 Mon Sep 17 00:00:00 2001 From: danielbprice Date: Wed, 15 Apr 2020 15:54:54 -0700 Subject: [PATCH] Add WithCaller(), a generalized AddCaller() (#806) Fixes #702. --- logger_test.go | 5 +++++ options.go | 11 +++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/logger_test.go b/logger_test.go index dabcb95a3..ed122e455 100644 --- a/logger_test.go +++ b/logger_test.go @@ -339,7 +339,12 @@ func TestLoggerAddCaller(t *testing.T) { options []Option pat string }{ + {opts(), `^undefined$`}, + {opts(WithCaller(false)), `^undefined$`}, {opts(AddCaller()), `.+/logger_test.go:[\d]+$`}, + {opts(AddCaller(), WithCaller(false)), `^undefined$`}, + {opts(WithCaller(true)), `.+/logger_test.go:[\d]+$`}, + {opts(WithCaller(true), WithCaller(false)), `^undefined$`}, {opts(AddCaller(), AddCallerSkip(1), AddCallerSkip(-1)), `.+/zap/logger_test.go:[\d]+$`}, {opts(AddCaller(), AddCallerSkip(1)), `.+/zap/common_test.go:[\d]+$`}, {opts(AddCaller(), AddCallerSkip(1), AddCallerSkip(3)), `.+/src/runtime/.*:[\d]+$`}, diff --git a/options.go b/options.go index dd3b6b2b2..59f1b54a0 100644 --- a/options.go +++ b/options.go @@ -87,10 +87,17 @@ func Development() Option { } // AddCaller configures the Logger to annotate each message with the filename -// and line number of zap's caller. +// and line number of zap's caller. See also WithCaller. func AddCaller() Option { + return WithCaller(true) +} + +// WithCaller configures the Logger to annotate each message with the filename +// and line number of zap's caller, or not, depending on the value of enabled. +// This is a generalized form of AddCaller. +func WithCaller(enabled bool) Option { return optionFunc(func(log *Logger) { - log.addCaller = true + log.addCaller = enabled }) }