-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Add DisableEscapeHTML option for json logger #690
Conversation
oops. sorry I break I think there are some ideas.
|
My initial thought is to call SetHTMLEscape(false) in jsonLogger without option. I'm not sure how much that is really a breaking change. The output will still be JSON compliant. I wouldn't consider the browser as a primary target for the output of jsonLogger. |
I think so too. so no one care about this change, call SetHTMLEscape(false) in jsonLogger seems to be better than others. |
This would read better if the property was named |
@peterbourgon how about this ? |
log/json_logger.go
Outdated
@@ -10,14 +10,29 @@ import ( | |||
|
|||
type jsonLogger struct { | |||
io.Writer | |||
disableEscapeHTML bool |
There was a problem hiding this comment.
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.
log/json_logger.go
Outdated
type JSONLoggerOption func(*jsonLogger) | ||
|
||
// DisableEscapeHTML disable to escape &, <, >. | ||
func DisableEscapeHTML(v bool) JSONLoggerOption { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log/json_logger.go
Outdated
@@ -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) |
There was a problem hiding this comment.
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)
@peterbourgon thanks for reviewing and change to SetEscapeHTML. Should I change like this ? (I think its not smart...) - format: log.NewJSONLogger,
+ format: func(w io.Writer) log.Logger { return log.NewJSONLogger(w) }, - logger := term.NewLogger(os.Stdout, log.NewJSONLogger, colorFn)
+ logger := term.NewLogger(os.Stdout, func(w io.Writer) log.Logger { return log.NewJSONLogger(w) }, colorFn) |
Seems like an OK solution. |
I'm not convinced yet. There is something very satisfying about the functions signatures of |
I could also see this being solved with e.g. type HTMLEscapingLogger struct {
next log.Logger
}
func (l HTMLEscapingLogger) Log(keyvals ...interface{}) error {
for i := 0; i < len(keyvals); i += 2 {
keyvals[i+1] = htmlEscape(keyvals[i+1])
}
l.next(keyvals...)
} |
Is it mean call |
@ChrisHines @peterbourgon it seems to be difficult both to keep |
I haven't seen any objections to just changing the existing JSONLogger to always call |
Hi.
I add DisableEscapeHTML option for json logger. If pass
log.DisableEscapeHTML(true)
to NewJSONLogger, we can get more readability.Thanks.