diff --git a/logger.go b/logger.go index 19ed409..bc1e769 100644 --- a/logger.go +++ b/logger.go @@ -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) { diff --git a/pkg.go b/pkg.go index 712bb38..5104375 100644 --- a/pkg.go +++ b/pkg.go @@ -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. diff --git a/text_test.go b/text_test.go index 12220ce..ef99a50 100644 --- a/text_test.go +++ b/text_test.go @@ -53,7 +53,7 @@ func TestTextCaller(t *testing.T) { }, { name: "helper caller", - expected: fmt.Sprintf("INFO info\n", filepath.Base(file), line+58), + expected: fmt.Sprintf("INFO info\n", filepath.Base(file), line+71), msg: "info", kvs: nil, f: func(msg interface{}, kvs ...interface{}) { @@ -76,7 +76,7 @@ func TestTextCaller(t *testing.T) { }, { name: "double nested helper caller", - expected: fmt.Sprintf("INFO info\n", filepath.Base(file), line+58), + expected: fmt.Sprintf("INFO info\n", filepath.Base(file), line+71), msg: "info", kvs: nil, f: func(msg interface{}, kvs ...interface{}) { @@ -88,6 +88,19 @@ func TestTextCaller(t *testing.T) { fun(msg, kvs...) }, }, + { + name: "encapsulated helper caller", + expected: fmt.Sprintf("INFO 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()