Skip to content

Commit

Permalink
Disable HTML escaping in JSON Logger (#914)
Browse files Browse the repository at this point in the history
  • Loading branch information
treuherz authored and ChrisHines committed Oct 7, 2019
1 parent 76396d1 commit 913ff7d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion log/json_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func (l *jsonLogger) Log(keyvals ...interface{}) error {
}
merge(m, k, v)
}
return json.NewEncoder(l.Writer).Encode(m)
enc := json.NewEncoder(l.Writer)
enc.SetEscapeHTML(false)
return enc.Encode(m)
}

func merge(dst map[string]interface{}, k, v interface{}) {
Expand Down
12 changes: 12 additions & 0 deletions log/json_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ func TestJSONLoggerNilErrorValue(t *testing.T) {
}
}

func TestJSONLoggerNoHTMLEscape(t *testing.T) {
t.Parallel()
buf := &bytes.Buffer{}
logger := log.NewJSONLogger(buf)
if err := logger.Log("k", "<&>"); err != nil {
t.Fatal(err)
}
if want, have := `{"k":"<&>"}`+"\n", buf.String(); want != have {
t.Errorf("\nwant %#v\nhave%#v", want, have)
}
}

// aller implements json.Marshaler, encoding.TextMarshaler, and fmt.Stringer.
type aller struct{}

Expand Down

0 comments on commit 913ff7d

Please sign in to comment.