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

Add DisableEscapeHTML option for json logger #690

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions log/json_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,29 @@ import (

type jsonLogger struct {
io.Writer
disableEscapeHTML bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be

escapeHTML bool

defaulting to false.

}

// NewJSONLogger returns a Logger that encodes keyvals to the Writer as a
// single JSON object. Each log event produces no more than one call to
// w.Write. The passed Writer must be safe for concurrent use by multiple
// goroutines if the returned Logger will be used concurrently.
func NewJSONLogger(w io.Writer) Logger {
return &jsonLogger{w}
func NewJSONLogger(w io.Writer, options ...JSONLoggerOption) Logger {
l := &jsonLogger{
Writer: w,
}
for _, option := range options {
option(l)
}
return l
}

// JSONLoggerOption sets a parameter for json logger
type JSONLoggerOption func(*jsonLogger)

// DisableEscapeHTML disable to escape &, <, >.
func DisableEscapeHTML(v bool) JSONLoggerOption {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be

func SetEscapeHTML(on bool) JSONLoggerOption {

mirroring json/Encoder.SetEscapeHTML.

return func(j *jsonLogger) { j.disableEscapeHTML = v }
}

func (l *jsonLogger) Log(keyvals ...interface{}) error {
Expand All @@ -31,7 +46,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(!l.disableEscapeHTML)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be

enc.SetEscapeHTML(l.escapeHTML)

return enc.Encode(m)
}

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

func TestJSONLoggerWithDisableHTMLEscapeOption(t *testing.T) {
t.Parallel()

buf := &bytes.Buffer{}
logger := log.NewJSONLogger(buf, log.DisableEscapeHTML(true))
if err := logger.Log("a", "<&>"); err != nil {
t.Fatal(err)
}
if want, have := `{"a":"<&>"}`+"\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