forked from mirror/logrus
Merge pull request #1113 from davidraleigh/html-escape
resolved conflicts for DisableHTMLEscape in json_formatter.go pull request #524
This commit is contained in:
commit
a635f0489d
|
@ -28,6 +28,9 @@ type JSONFormatter struct {
|
|||
// DisableTimestamp allows disabling automatic timestamps in output
|
||||
DisableTimestamp bool
|
||||
|
||||
// DisableHTMLEscape allows disabling html escaping in output
|
||||
DisableHTMLEscape bool
|
||||
|
||||
// DataKey allows users to put all the log entry parameters into a nested dictionary at a given key.
|
||||
DataKey string
|
||||
|
||||
|
@ -110,6 +113,7 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
|
|||
}
|
||||
|
||||
encoder := json.NewEncoder(b)
|
||||
encoder.SetEscapeHTML(!f.DisableHTMLEscape)
|
||||
if f.PrettyPrint {
|
||||
encoder.SetIndent("", " ")
|
||||
}
|
||||
|
|
|
@ -344,3 +344,29 @@ func TestJSONEnableTimestamp(t *testing.T) {
|
|||
t.Error("Timestamp not present", s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONDisableHTMLEscape(t *testing.T) {
|
||||
formatter := &JSONFormatter{DisableHTMLEscape: true}
|
||||
|
||||
b, err := formatter.Format(&Entry{Message: "& < >"})
|
||||
if err != nil {
|
||||
t.Fatal("Unable to format entry: ", err)
|
||||
}
|
||||
s := string(b)
|
||||
if !strings.Contains(s, "& < >") {
|
||||
t.Error("Message should not be HTML escaped", s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONEnableHTMLEscape(t *testing.T) {
|
||||
formatter := &JSONFormatter{}
|
||||
|
||||
b, err := formatter.Format(&Entry{Message: "& < >"})
|
||||
if err != nil {
|
||||
t.Fatal("Unable to format entry: ", err)
|
||||
}
|
||||
s := string(b)
|
||||
if !(strings.Contains(s, "u0026") && strings.Contains(s, "u003e") && strings.Contains(s, "u003c")) {
|
||||
t.Error("Message should be HTML escaped", s)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue