feat: optimize logx print error (#3649)
This commit is contained in:
@@ -7,10 +7,13 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"path"
|
"path"
|
||||||
|
"reflect"
|
||||||
|
"runtime/debug"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
fatihcolor "github.com/fatih/color"
|
fatihcolor "github.com/fatih/color"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/color"
|
"github.com/zeromicro/go-zero/core/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -332,7 +335,7 @@ 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.Println(err.Error())
|
log.Printf("err: %s, type: %s\n\n%s\n", err.Error(), reflect.TypeOf(info).Name(), debug.Stack())
|
||||||
} else if writer == nil {
|
} else if writer == nil {
|
||||||
log.Println(string(content))
|
log.Println(string(content))
|
||||||
} else {
|
} else {
|
||||||
@@ -384,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.Println(err.Error())
|
log.Printf("err: %s, type: %s\n\n%s\n", err.Error(), reflect.TypeOf(val).Name(), debug.Stack())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -129,6 +129,15 @@ func TestWriteJson(t *testing.T) {
|
|||||||
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")
|
||||||
|
|
||||||
|
buf.Reset()
|
||||||
|
type C struct {
|
||||||
|
RC func()
|
||||||
|
}
|
||||||
|
writeJson(nil, C{
|
||||||
|
RC: func() {},
|
||||||
|
})
|
||||||
|
assert.Contains(t, buf.String(), "runtime/debug.Stack")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWritePlainAny(t *testing.T) {
|
func TestWritePlainAny(t *testing.T) {
|
||||||
@@ -165,6 +174,14 @@ func TestWritePlainAny(t *testing.T) {
|
|||||||
writePlainAny(hardToWriteWriter{}, levelFatal, "foo")
|
writePlainAny(hardToWriteWriter{}, levelFatal, "foo")
|
||||||
assert.Contains(t, buf.String(), "write error")
|
assert.Contains(t, buf.String(), "write error")
|
||||||
|
|
||||||
|
buf.Reset()
|
||||||
|
type C struct {
|
||||||
|
RC func()
|
||||||
|
}
|
||||||
|
writePlainAny(nil, levelError, C{
|
||||||
|
RC: func() {},
|
||||||
|
})
|
||||||
|
assert.Contains(t, buf.String(), "runtime/debug.Stack")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLogWithLimitContentLength(t *testing.T) {
|
func TestLogWithLimitContentLength(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user