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 parameter to JSONFormatter #982

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions json_formatter.go
Expand Up @@ -51,6 +51,9 @@ type JSONFormatter struct {

// PrettyPrint will indent all json logs
PrettyPrint bool

// DisableEscapeHTML allows disabling HTML escape.
DisableEscapeHTML bool
}

// Format renders a single log entry
Expand Down Expand Up @@ -110,6 +113,9 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
}

encoder := json.NewEncoder(b)
if f.DisableEscapeHTML {
encoder.SetEscapeHTML(false)
}
if f.PrettyPrint {
encoder.SetIndent("", " ")
}
Expand Down
29 changes: 29 additions & 0 deletions json_formatter_test.go
Expand Up @@ -344,3 +344,32 @@ func TestJSONEnableTimestamp(t *testing.T) {
t.Error("Timestamp not present", s)
}
}

func TestJSONDisableEscapeHTML(t *testing.T) {
formatter := &JSONFormatter{
DisableEscapeHTML: true,
}

b, err := formatter.Format(WithField("xml", "<xml>&</xml>"))
if err != nil {
t.Fatal("Unable to format entry: ", err)
}
s := string(b)
if !strings.Contains(s, "<xml>&</xml>") {
t.Error("Raw keyword does not find", s)
}
}

func TestJSONEnableEscapeHTML(t *testing.T) {
formatter := &JSONFormatter{}

b, err := formatter.Format(WithField("xml", "<xml>&</xml>"))
if err != nil {
t.Fatal("Unable to format entry: ", err)
}
s := string(b)
escaped := `\u003cxml\u003e\u0026\u003c/xml\u003e` // same `<xml>&</xml>`
if !strings.Contains(s, escaped) {
t.Error("Escaped keyword does not find", s)
}
}