Skip to content

Commit

Permalink
Merge pull request #141 from Ericwww/support-json-escape
Browse files Browse the repository at this point in the history
support configure json escape when log in json format
  • Loading branch information
evanphx committed Apr 1, 2024
2 parents 5dbb615 + cb8687c commit d12136a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
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

0 comments on commit d12136a

Please sign in to comment.