From 32600f2619684a2a67ca6ce0e4085cbb27021d85 Mon Sep 17 00:00:00 2001 From: MarkJoyMa <64180138+MarkJoyMa@users.noreply.github.com> Date: Sat, 28 Oct 2023 19:46:52 +0800 Subject: [PATCH] fix: adjust log encode output mode (#3676) --- core/logx/writer.go | 9 +++++---- core/logx/writer_test.go | 5 +++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/core/logx/writer.go b/core/logx/writer.go index 1d60a5bd..ac19aa24 100644 --- a/core/logx/writer.go +++ b/core/logx/writer.go @@ -7,7 +7,6 @@ import ( "io" "log" "path" - "reflect" "runtime/debug" "sync" "sync/atomic" @@ -334,11 +333,13 @@ func wrapLevelWithColor(level string) string { func writeJson(writer io.Writer, info any) { if content, err := json.Marshal(info); err != nil { - log.Printf("err: %s, type: %s\n\n%s\n", err.Error(), reflect.TypeOf(info).Name(), debug.Stack()) + log.Printf("err: %s\n\n%s", err.Error(), debug.Stack()) } else if writer == nil { log.Println(string(content)) } else { - writer.Write(append(content, '\n')) + if _, err := writer.Write(append(content, '\n')); err != nil { + log.Println(err.Error()) + } } } @@ -386,7 +387,7 @@ func writePlainValue(writer io.Writer, level string, val any, fields ...string) buf.WriteString(level) buf.WriteByte(plainEncodingSep) if err := json.NewEncoder(&buf).Encode(val); err != nil { - log.Printf("err: %s, type: %s\n\n%s\n", err.Error(), reflect.TypeOf(val).Name(), debug.Stack()) + log.Printf("err: %s\n\n%s", err.Error(), debug.Stack()) return } diff --git a/core/logx/writer_test.go b/core/logx/writer_test.go index c2d55aec..0f810975 100644 --- a/core/logx/writer_test.go +++ b/core/logx/writer_test.go @@ -126,6 +126,11 @@ func TestWriteJson(t *testing.T) { log.SetOutput(&buf) writeJson(nil, "foo") assert.Contains(t, buf.String(), "foo") + + buf.Reset() + writeJson(hardToWriteWriter{}, "foo") + assert.Contains(t, buf.String(), "write error") + buf.Reset() writeJson(nil, make(chan int)) assert.Contains(t, buf.String(), "unsupported type")