Skip to content

Commit

Permalink
Disable HTML escaping in JSON Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
treuherz committed Sep 27, 2019
1 parent dc489b7 commit 641cc6c
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 641cc6c

Please sign in to comment.