fix: adjust log encode output mode (#3676)
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"path"
|
"path"
|
||||||
"reflect"
|
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@@ -334,11 +333,13 @@ func wrapLevelWithColor(level string) string {
|
|||||||
|
|
||||||
func writeJson(writer io.Writer, info any) {
|
func writeJson(writer io.Writer, info any) {
|
||||||
if content, err := json.Marshal(info); err != nil {
|
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 {
|
} else if writer == nil {
|
||||||
log.Println(string(content))
|
log.Println(string(content))
|
||||||
} else {
|
} 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.WriteString(level)
|
||||||
buf.WriteByte(plainEncodingSep)
|
buf.WriteByte(plainEncodingSep)
|
||||||
if err := json.NewEncoder(&buf).Encode(val); err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -126,6 +126,11 @@ func TestWriteJson(t *testing.T) {
|
|||||||
log.SetOutput(&buf)
|
log.SetOutput(&buf)
|
||||||
writeJson(nil, "foo")
|
writeJson(nil, "foo")
|
||||||
assert.Contains(t, buf.String(), "foo")
|
assert.Contains(t, buf.String(), "foo")
|
||||||
|
|
||||||
|
buf.Reset()
|
||||||
|
writeJson(hardToWriteWriter{}, "foo")
|
||||||
|
assert.Contains(t, buf.String(), "write error")
|
||||||
|
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
writeJson(nil, make(chan int))
|
writeJson(nil, make(chan int))
|
||||||
assert.Contains(t, buf.String(), "unsupported type")
|
assert.Contains(t, buf.String(), "unsupported type")
|
||||||
|
|||||||
Reference in New Issue
Block a user