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

Fix panic in log with empty header #51

Merged
merged 1 commit into from Jun 14, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions log/log.go
Expand Up @@ -391,7 +391,7 @@ func (l *Logger) log(level Lvl, format string, args ...interface{}) {
if err == nil {
s := buf.String()
i := buf.Len() - 1
if s[i] == '}' {
if i >= 0 && s[i] == '}' {
// JSON header
buf.Truncate(i)
buf.WriteByte(',')
Expand All @@ -404,7 +404,9 @@ func (l *Logger) log(level Lvl, format string, args ...interface{}) {
}
} else {
// Text header
buf.WriteByte(' ')
if len(s) > 0 {
buf.WriteByte(' ')
}
buf.WriteString(message)
}
buf.WriteByte('\n')
Expand Down
10 changes: 10 additions & 0 deletions log/log_test.go
Expand Up @@ -119,6 +119,16 @@ func TestStringWithQuotes(t *testing.T) {
assert.Contains(t, b.String(), `"message":"Content-Type: \"\""`)
}

func TestEmptyHeader(t *testing.T) {
l := New("test")
b := new(bytes.Buffer)
l.SetOutput(b)
l.SetHeader("")
l.SetLevel(DEBUG)
l.Debugf("captain's log")
assert.Contains(t, b.String(), `captain's log`)
}

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