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

support configure json escape when log in json format #141

Merged
merged 1 commit into from
Apr 1, 2024
Merged
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
22 changes: 14 additions & 8 deletions intlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@ var _ Logger = &intLogger{}
// intLogger is an internal logger implementation. Internal in that it is
// defined entirely by this package.
type intLogger struct {
json bool
callerOffset int
name string
timeFormat string
timeFn TimeFunction
disableTime bool
json bool
jsonEscapeEnabled bool
callerOffset int
name string
timeFormat string
timeFn TimeFunction
disableTime bool

// This is an interface so that it's shared by any derived loggers, since
// those derived loggers share the bufio.Writer as well.
Expand Down Expand Up @@ -173,6 +174,7 @@ func newLogger(opts *LoggerOptions) *intLogger {

l := &intLogger{
json: opts.JSONFormat,
jsonEscapeEnabled: !opts.JSONEscapeDisabled,
name: opts.Name,
timeFormat: TimeFormat,
timeFn: time.Now,
Expand Down Expand Up @@ -667,13 +669,17 @@ func (l *intLogger) logJSON(t time.Time, name string, level Level, msg string, a
}
}

err := json.NewEncoder(l.writer).Encode(vals)
encoder := json.NewEncoder(l.writer)
encoder.SetEscapeHTML(l.jsonEscapeEnabled)
err := encoder.Encode(vals)
if err != nil {
if _, ok := err.(*json.UnsupportedTypeError); ok {
plainVal := l.jsonMapEntry(t, name, level, msg)
plainVal["@warn"] = errJsonUnsupportedTypeMsg

json.NewEncoder(l.writer).Encode(plainVal)
errEncoder := json.NewEncoder(l.writer)
errEncoder.SetEscapeHTML(l.jsonEscapeEnabled)
errEncoder.Encode(plainVal)
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ type LoggerOptions struct {
// Control if the output should be in JSON.
JSONFormat bool

// Control the escape switch of json.Encoder
JSONEscapeDisabled bool

// Include file and line information in each log line
IncludeLocation bool

Expand Down
22 changes: 22 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,28 @@ func TestLogger_JSON(t *testing.T) {
assert.Equal(t, "[INFO] test: who=programmer why=testing\n", rest)
})

t.Run("disable json escape when log special character", func(t *testing.T) {
var buf bytes.Buffer

logger := New(&LoggerOptions{
Name: "test",
Output: &buf,
JSONFormat: true,
JSONEscapeDisabled: true,
})

logger.Info("this is test and use > < &")

b := buf.Bytes()

var raw map[string]interface{}
if err := json.Unmarshal(b, &raw); err != nil {
t.Fatal(err)
}

assert.Equal(t, "this is test and use > < &", raw["@message"])
})

}

type customErrJSON struct {
Expand Down