Skip to content

Commit

Permalink
Add SetCaller, a generalized AddCaller()
Browse files Browse the repository at this point in the history
Fixes #702.
  • Loading branch information
danielbprice committed Apr 3, 2020
1 parent f994215 commit 49db921
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
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(SetCaller(false)), `^undefined$`},
{opts(AddCaller()), `.+/logger_test.go:[\d]+$`},
{opts(AddCaller(), SetCaller(false)), `^undefined$`},
{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]+$`},
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 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
})
}

Expand Down

0 comments on commit 49db921

Please sign in to comment.