From 1edff8beee5b3ee535db45135e1817fb2ab5f9c1 Mon Sep 17 00:00:00 2001 From: Daniel Price Date: Thu, 26 Mar 2020 20:29:37 +0000 Subject: [PATCH] Add SetCaller, a generalized AddCaller() Fixes #702. --- logger_test.go | 4 ++++ options.go | 11 +++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/logger_test.go b/logger_test.go index dabcb95a3..cb19b9826 100644 --- a/logger_test.go +++ b/logger_test.go @@ -339,7 +339,11 @@ func TestLoggerAddCaller(t *testing.T) { options []Option pat string }{ + {opts(), `^undefined$`}, + {opts(SetCaller(false)), `^undefined$`}, {opts(AddCaller()), `.+/logger_test.go:[\d]+$`}, + {opts(SetCaller(true)), `.+/logger_test.go:[\d]+$`}, + {opts(SetCaller(true), SetCaller(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..6ae475f39 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 SetCaller. func AddCaller() Option { + return SetCaller(true) +} + +// SetCaller 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 SetCaller(enabled bool) Option { return optionFunc(func(log *Logger) { - log.addCaller = true + log.addCaller = enabled }) }