From d5c9a7ada30d5b29760b3eec611ccf32c41fb503 Mon Sep 17 00:00:00 2001 From: Eli Treuherz Date: Fri, 27 Sep 2019 10:26:01 +0100 Subject: [PATCH] Disable HTML escaping in JSON Logger --- log/json_logger.go | 4 +++- log/json_logger_test.go | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/log/json_logger.go b/log/json_logger.go index 66094b4dd..0cedbf824 100644 --- a/log/json_logger.go +++ b/log/json_logger.go @@ -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{}) { diff --git a/log/json_logger_test.go b/log/json_logger_test.go index e3e309090..6432fc837 100644 --- a/log/json_logger_test.go +++ b/log/json_logger_test.go @@ -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{}