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
16 changes: 16 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ func (l *Logger) SetHeader(h string) {
l.template = l.newTemplate(h)
}

func (l *Logger) HeaderSkip() int {
return l.skip
}

func (l *Logger) SetHeaderSkip(skip int) {
l.skip = skip
}

func (l *Logger) Print(i ...interface{}) {
l.log(0, fmt.Sprint(i...), false)
}
Expand Down Expand Up @@ -265,6 +273,14 @@ func SetHeader(h string) {
global.SetHeader(h)
}

func HeaderSkip() int {
return global.HeaderSkip()
}

func SetHeaderSkip(skip int) {
global.SetHeaderSkip(skip)
}

func Print(i ...interface{}) {
global.Print(i...)
}
Expand Down
24 changes: 24 additions & 0 deletions log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,30 @@ func TestCallerFile(t *testing.T) {
assert.Contains(t, b.String(), "log_test.go", "JSON path should report caller file")
}

func TestConfigurableHeaderSkip(t *testing.T) {
l := New("test")
l.SetHeader("${short_file}")
b := new(bytes.Buffer)
l.SetOutput(b)
l.SetLevel(DEBUG)

assert.Equal(t, 2, l.HeaderSkip())

// Wrapping function that logs
var wrapperLog = func(msg string) {
l.Info(msg)
}

// Without adjusting skip, wrapperLog reports log_test.go inside wrapperLog (or test file depending on frames)
// Let's test custom skip
l.SetHeaderSkip(l.HeaderSkip() + 1)
assert.Equal(t, 3, l.HeaderSkip())

b.Reset()
wrapperLog("wrapped call")
assert.Contains(t, b.String(), "log_test.go")
}

func BenchmarkLog(b *testing.B) {
l := New("test")
l.SetOutput(new(bytes.Buffer))
Expand Down