Skip to content

Commit 1661db9

Browse files
committed
Add WithCaller(), a generalized AddCaller()
Fixes #702.
1 parent f994215 commit 1661db9

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

logger_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,12 @@ func TestLoggerAddCaller(t *testing.T) {
339339
options []Option
340340
pat string
341341
}{
342+
{opts(), `^undefined$`},
343+
{opts(WithCaller(false)), `^undefined$`},
342344
{opts(AddCaller()), `.+/logger_test.go:[\d]+$`},
345+
{opts(AddCaller(), WithCaller(false)), `^undefined$`},
346+
{opts(WithCaller(true)), `.+/logger_test.go:[\d]+$`},
347+
{opts(WithCaller(true), WithCaller(false)), `^undefined$`},
343348
{opts(AddCaller(), AddCallerSkip(1), AddCallerSkip(-1)), `.+/zap/logger_test.go:[\d]+$`},
344349
{opts(AddCaller(), AddCallerSkip(1)), `.+/zap/common_test.go:[\d]+$`},
345350
{opts(AddCaller(), AddCallerSkip(1), AddCallerSkip(3)), `.+/src/runtime/.*:[\d]+$`},

options.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,17 @@ func Development() Option {
8787
}
8888

8989
// AddCaller configures the Logger to annotate each message with the filename
90-
// and line number of zap's caller.
90+
// and line number of zap's caller. See also WithCaller.
9191
func AddCaller() Option {
92+
return WithCaller(true)
93+
}
94+
95+
// WithCaller configures the Logger to annotate each message with the filename
96+
// and line number of zap's caller, or not, depending on the value of enabled.
97+
// This is a generalized form of AddCaller.
98+
func WithCaller(enabled bool) Option {
9299
return optionFunc(func(log *Logger) {
93-
log.addCaller = true
100+
log.addCaller = enabled
94101
})
95102
}
96103

0 commit comments

Comments
 (0)