Skip to content

Commit

Permalink
Escape strings with quotes (labstack#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
asib authored and vishr committed Dec 20, 2016
1 parent 447fe74 commit 7355c05
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 3 additions & 3 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,9 @@ func (l *Logger) log(v Lvl, format string, args ...interface{}) {
if format == "json" {
buf.WriteString(message[1:])
} else {
buf.WriteString(`"message":"`)
buf.WriteString(message)
buf.WriteString(`"}`)
buf.WriteString(`"message":`)
buf.WriteString(strconv.Quote(message))
buf.WriteString(`}`)
}
} else {
// Text header
Expand Down
9 changes: 9 additions & 0 deletions log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ func TestJSON(t *testing.T) {
assert.Contains(t, b.String(), `"name":"value"`)
}

func TestStringWithQuotes(t *testing.T) {
l := New("test")
b := new(bytes.Buffer)
l.SetOutput(b)
l.SetLevel(DEBUG)
l.Debugf("Content-Type: %q", "")
assert.Contains(t, b.String(), `"message":"Content-Type: \"\""`)
}

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

0 comments on commit 7355c05

Please sign in to comment.