chore: refactor logx (#2262)

master
Kevin Wan 2 years ago committed by GitHub
parent 9af1a42386
commit 05dd6bd743
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -307,29 +307,7 @@ func writePlainAny(writer io.Writer, level string, val interface{}, fields ...st
case fmt.Stringer: case fmt.Stringer:
writePlainText(writer, level, v.String(), fields...) writePlainText(writer, level, v.String(), fields...)
default: default:
var buf strings.Builder writePlainValue(writer, level, v, fields...)
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())
}
} }
} }
@ -354,3 +332,29 @@ func writePlainText(writer io.Writer, level, msg string, fields ...string) {
log.Println(err.Error()) 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())
}
}

Loading…
Cancel
Save