Skip to content

Commit cb8687c

Browse files
committed
support configure json escape when log in json format
1 parent 5dbb615 commit cb8687c

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

intlogger.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,13 @@ var _ Logger = &intLogger{}
8080
// intLogger is an internal logger implementation. Internal in that it is
8181
// defined entirely by this package.
8282
type intLogger struct {
83-
json bool
84-
callerOffset int
85-
name string
86-
timeFormat string
87-
timeFn TimeFunction
88-
disableTime bool
83+
json bool
84+
jsonEscapeEnabled bool
85+
callerOffset int
86+
name string
87+
timeFormat string
88+
timeFn TimeFunction
89+
disableTime bool
8990

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

174175
l := &intLogger{
175176
json: opts.JSONFormat,
177+
jsonEscapeEnabled: !opts.JSONEscapeDisabled,
176178
name: opts.Name,
177179
timeFormat: TimeFormat,
178180
timeFn: time.Now,
@@ -667,13 +669,17 @@ func (l *intLogger) logJSON(t time.Time, name string, level Level, msg string, a
667669
}
668670
}
669671

670-
err := json.NewEncoder(l.writer).Encode(vals)
672+
encoder := json.NewEncoder(l.writer)
673+
encoder.SetEscapeHTML(l.jsonEscapeEnabled)
674+
err := encoder.Encode(vals)
671675
if err != nil {
672676
if _, ok := err.(*json.UnsupportedTypeError); ok {
673677
plainVal := l.jsonMapEntry(t, name, level, msg)
674678
plainVal["@warn"] = errJsonUnsupportedTypeMsg
675679

676-
json.NewEncoder(l.writer).Encode(plainVal)
680+
errEncoder := json.NewEncoder(l.writer)
681+
errEncoder.SetEscapeHTML(l.jsonEscapeEnabled)
682+
errEncoder.Encode(plainVal)
677683
}
678684
}
679685
}

logger.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ type LoggerOptions struct {
264264
// Control if the output should be in JSON.
265265
JSONFormat bool
266266

267+
// Control the escape switch of json.Encoder
268+
JSONEscapeDisabled bool
269+
267270
// Include file and line information in each log line
268271
IncludeLocation bool
269272

logger_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,28 @@ func TestLogger_JSON(t *testing.T) {
12321232
assert.Equal(t, "[INFO] test: who=programmer why=testing\n", rest)
12331233
})
12341234

1235+
t.Run("disable json escape when log special character", func(t *testing.T) {
1236+
var buf bytes.Buffer
1237+
1238+
logger := New(&LoggerOptions{
1239+
Name: "test",
1240+
Output: &buf,
1241+
JSONFormat: true,
1242+
JSONEscapeDisabled: true,
1243+
})
1244+
1245+
logger.Info("this is test and use > < &")
1246+
1247+
b := buf.Bytes()
1248+
1249+
var raw map[string]interface{}
1250+
if err := json.Unmarshal(b, &raw); err != nil {
1251+
t.Fatal(err)
1252+
}
1253+
1254+
assert.Equal(t, "this is test and use > < &", raw["@message"])
1255+
})
1256+
12351257
}
12361258

12371259
type customErrJSON struct {

0 commit comments

Comments
 (0)