Skip to content

Commit dbd14a5

Browse files
authored
Fix the output is incorrect when writing an error log with arguments that contain the format character (#11)
1 parent 11408c0 commit dbd14a5

File tree

4 files changed

+4
-2
lines changed

4 files changed

+4
-2
lines changed

formatter/json/json.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func newJsonFormatter() formatter.Formatter {
1919
func (f *jsonFormatter) Serialize(c content.Content) ([]byte, error) {
2020
c.Log = fmt.Sprintf(c.Log, c.Args...)
2121
if c.Error != nil {
22-
c.Log = fmt.Sprintf(c.Log+". %s", c.Error)
22+
c.Log = c.Log + ". " + c.Error.Error()
2323
}
2424
buf := new(bytes.Buffer)
2525
encoder := json.NewEncoder(buf)

formatter/json/json_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func TestJsonFormatter_Serialize(t *testing.T) {
2727
{"debug with args and append time", content.NewContentWithTime(level.DebugLevel, nil, logTimeP, "<json formatter> %s %s", "hello", "world"), `{"level":"DEBUG","time":"2022-06-25 23:59:59","log":"<json formatter> hello world"}`},
2828
{"error no args and append time", content.NewContentWithTime(level.DebugLevel, errors.New("test error"), logTimeP, "<json formatter> hello"), `{"level":"DEBUG","time":"2022-06-25 23:59:59","log":"<json formatter> hello. test error"}`},
2929
{"error with args and append time", content.NewContentWithTime(level.DebugLevel, errors.New("test error"), logTimeP, "<json formatter> %s %s", "hello", "world"), `{"level":"DEBUG","time":"2022-06-25 23:59:59","log":"<json formatter> hello world. test error"}`},
30+
{"error with args that contain the format character", content.NewContentWithTime(level.DebugLevel, errors.New("test error"), logTimeP, "<json formatter> %s %s", "hello%s%v", "world"), `{"level":"DEBUG","time":"2022-06-25 23:59:59","log":"<json formatter> hello%s%v world. test error"}`},
3031
}
3132
f := newJsonFormatter()
3233
for _, tc := range testCases {

formatter/text/text.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (f *textFormatter) Serialize(c content.Content) ([]byte, error) {
2323
content := fmt.Sprintf(c.Log, c.Args...)
2424
format = fmt.Sprintf(format, timeSection, c.Level.String(), content)
2525
if c.Error != nil {
26-
format = fmt.Sprintf(format+". %s", c.Error)
26+
format = format + ". " + c.Error.Error()
2727
}
2828
format = formatter.AppendRowTerminator(format)
2929
return []byte(format), nil

formatter/text/text_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func TestTextFormatter_Serialize(t *testing.T) {
2727
{"debug with args and append time", content.NewContentWithTime(level.DebugLevel, nil, logTimeP, "<text formatter> %s %s", "hello", "world"), "[2022-06-25 23:59:59] [DEBUG] <text formatter> hello world"},
2828
{"error no args and append time", content.NewContentWithTime(level.DebugLevel, errors.New("test error"), logTimeP, "<text formatter> hello"), "[2022-06-25 23:59:59] [DEBUG] <text formatter> hello. test error"},
2929
{"error with args and append time", content.NewContentWithTime(level.DebugLevel, errors.New("test error"), logTimeP, "<text formatter> %s %s", "hello", "world"), "[2022-06-25 23:59:59] [DEBUG] <text formatter> hello world. test error"},
30+
{"error with args that contain the format character", content.NewContentWithTime(level.DebugLevel, errors.New("test error"), logTimeP, "<text formatter> %s %s", "hello%s%v", "world"), "[2022-06-25 23:59:59] [DEBUG] <text formatter> hello%s%v world. test error"},
3031
}
3132
f := newTextFormatter()
3233
for _, tc := range testCases {

0 commit comments

Comments
 (0)