Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WithCaller(), a generalized AddCaller() #806

Merged
merged 1 commit into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]+$`},
Expand Down
11 changes: 9 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}

Expand Down