- add Formatter.JSONEscapeHTML and SetJSONEscapeHTML to let JSON writers preserve HTML-sensitive characters when needed while keeping escaping enabled by default. - switch JSON output to json.Encoder, preserve existing Print/Println newline semantics, and cover the new escaping behavior in tests.
This commit is contained in:
+18
-3
@@ -32,9 +32,10 @@ type Formatter struct {
|
||||
Format string
|
||||
MessageSeparator string
|
||||
|
||||
// JSONTraceBackPath
|
||||
// If true, in traceback will be path to file, otherwise only filename.
|
||||
JSONTraceBackPath bool
|
||||
// JSONTraceBackPath enables full file paths in formatted traceback fields.
|
||||
JSONTraceBackPath bool
|
||||
// JSONEscapeHTML controls whether JSON output escapes HTML-sensitive characters.
|
||||
JSONEscapeHTML bool
|
||||
TimeStampFormat string
|
||||
TraceBackFormat string
|
||||
TraceBackSeparator string
|
||||
@@ -57,6 +58,7 @@ var DefaultTextFormatter = &Formatter{
|
||||
// DefaultJsonFormatter is the default formatter used by JSON writers.
|
||||
var DefaultJsonFormatter = &Formatter{
|
||||
Format: "%m",
|
||||
JSONEscapeHTML: true,
|
||||
MessageSeparator: " ",
|
||||
TimeStampFormat: RFC3339,
|
||||
ColorOutput: false,
|
||||
@@ -67,6 +69,7 @@ func NewFormatter() *Formatter {
|
||||
return &Formatter{
|
||||
Format: DefaultTextFormatter.Format,
|
||||
MessageSeparator: DefaultTextFormatter.MessageSeparator,
|
||||
JSONEscapeHTML: true,
|
||||
TimeStampFormat: DefaultTextFormatter.TimeStampFormat,
|
||||
TraceBackFormat: DefaultTextFormatter.TraceBackFormat,
|
||||
TraceBackSeparator: DefaultTextFormatter.TraceBackSeparator,
|
||||
@@ -87,6 +90,18 @@ func (f *Formatter) SetMessageSeparator(separator string) *Formatter {
|
||||
return f
|
||||
}
|
||||
|
||||
// SetJSONTraceBackPath enables or disables full file paths in formatted traceback fields.
|
||||
func (f *Formatter) SetJSONTraceBackPath(set bool) *Formatter {
|
||||
f.JSONTraceBackPath = set
|
||||
return f
|
||||
}
|
||||
|
||||
// SetJSONEscapeHTML enables or disables HTML escaping in JSON output.
|
||||
func (f *Formatter) SetJSONEscapeHTML(escape bool) *Formatter {
|
||||
f.JSONEscapeHTML = escape
|
||||
return f
|
||||
}
|
||||
|
||||
// SetTimeStampFormat sets the strftime-like format used for timestamps.
|
||||
func (f *Formatter) SetTimeStampFormat(format string) *Formatter {
|
||||
f.TimeStampFormat = format
|
||||
|
||||
Reference in New Issue
Block a user