From 05dd6bd743f347766576e494125c605c7630713d Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sun, 14 Aug 2022 13:58:06 +0800 Subject: [PATCH] chore: refactor logx (#2262) --- core/logx/writer.go | 50 ++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/core/logx/writer.go b/core/logx/writer.go index 485736d4..f83dd995 100644 --- a/core/logx/writer.go +++ b/core/logx/writer.go @@ -307,29 +307,7 @@ func writePlainAny(writer io.Writer, level string, val interface{}, fields ...st case fmt.Stringer: writePlainText(writer, level, v.String(), fields...) default: - var buf strings.Builder - buf.WriteString(getTimestamp()) - buf.WriteByte(plainEncodingSep) - buf.WriteString(level) - buf.WriteByte(plainEncodingSep) - if err := json.NewEncoder(&buf).Encode(val); err != nil { - log.Println(err.Error()) - return - } - - for _, item := range fields { - buf.WriteByte(plainEncodingSep) - buf.WriteString(item) - } - buf.WriteByte('\n') - if writer == nil { - log.Println(buf.String()) - return - } - - if _, err := fmt.Fprint(writer, buf.String()); err != nil { - log.Println(err.Error()) - } + writePlainValue(writer, level, v, fields...) } } @@ -354,3 +332,29 @@ func writePlainText(writer io.Writer, level, msg string, fields ...string) { log.Println(err.Error()) } } + +func writePlainValue(writer io.Writer, level string, val interface{}, fields ...string) { + var buf strings.Builder + buf.WriteString(getTimestamp()) + buf.WriteByte(plainEncodingSep) + buf.WriteString(level) + buf.WriteByte(plainEncodingSep) + if err := json.NewEncoder(&buf).Encode(val); err != nil { + log.Println(err.Error()) + return + } + + for _, item := range fields { + buf.WriteByte(plainEncodingSep) + buf.WriteString(item) + } + buf.WriteByte('\n') + if writer == nil { + log.Println(buf.String()) + return + } + + if _, err := fmt.Fprint(writer, buf.String()); err != nil { + log.Println(err.Error()) + } +}