Skip to content
Open
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
12 changes: 10 additions & 2 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,16 @@ func (l *Logger) handle(level Level, ts time.Time, frames []runtime.Frame, msg i
// Helper marks the calling function as a helper
// and skips it for source location information.
// It's the equivalent of testing.TB.Helper().
func (l *Logger) Helper() {
l.helper(1)
//
// It takes an optional argument skip,
// which is the depth of the call stack that should be skipped.
// In most cases you should omit providing it and it will default to 1.
func (l *Logger) Helper(skip ...int) {
if len(skip) > 0 {
l.helper(skip[0])
} else {
l.helper(1)
}
}

func (l *Logger) helper(skip int) {
Expand Down
12 changes: 10 additions & 2 deletions pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,16 @@ func WithPrefix(prefix string) *Logger {
// Helper marks the calling function as a helper
// and skips it for source location information.
// It's the equivalent of testing.TB.Helper().
func Helper() {
Default().helper(1)
//
// It takes an optional argument skip,
// which is the depth of the call stack that should be skipped.
// In most cases you should omit providing it and it will default to 1.
func Helper(skip ...int) {
if len(skip) > 0 {
Default().helper(skip[0])
} else {
Default().helper(1)
}
}

// Log logs a message with the given level.
Expand Down
17 changes: 15 additions & 2 deletions text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestTextCaller(t *testing.T) {
},
{
name: "helper caller",
expected: fmt.Sprintf("INFO <log/%s:%d> info\n", filepath.Base(file), line+58),
expected: fmt.Sprintf("INFO <log/%s:%d> info\n", filepath.Base(file), line+71),
msg: "info",
kvs: nil,
f: func(msg interface{}, kvs ...interface{}) {
Expand All @@ -76,7 +76,7 @@ func TestTextCaller(t *testing.T) {
},
{
name: "double nested helper caller",
expected: fmt.Sprintf("INFO <log/%s:%d> info\n", filepath.Base(file), line+58),
expected: fmt.Sprintf("INFO <log/%s:%d> info\n", filepath.Base(file), line+71),
msg: "info",
kvs: nil,
f: func(msg interface{}, kvs ...interface{}) {
Expand All @@ -88,6 +88,19 @@ func TestTextCaller(t *testing.T) {
fun(msg, kvs...)
},
},
{
name: "encapsulated helper caller",
expected: fmt.Sprintf("INFO <log/%s:%d> info\n", filepath.Base(file), line+71),
msg: "info",
kvs: nil,
f: func(msg interface{}, kvs ...interface{}) {
encapsulatedHelper := func() {
logger.Helper(2)
}
encapsulatedHelper()
logger.Info(msg, kvs...)
},
},
}
for _, c := range cases {
buf.Reset()
Expand Down