diff --git a/core/logx/logs.go b/core/logx/logs.go index 57649e65..9cf3a791 100644 --- a/core/logx/logs.go +++ b/core/logx/logs.go @@ -433,7 +433,7 @@ func encodeWithRecover(arg any, fn func() string) (ret string) { if v := reflect.ValueOf(arg); v.Kind() == reflect.Ptr && v.IsNil() { ret = nilAngleString } else { - panic(err) + ret = fmt.Sprintf("panic: %v", err) } } }() diff --git a/core/logx/logs_test.go b/core/logx/logs_test.go index 7622453b..5d46b7b9 100644 --- a/core/logx/logs_test.go +++ b/core/logx/logs_test.go @@ -348,15 +348,11 @@ func TestStructedLogInfow(t *testing.T) { }) } -func TestStructedLogInfowNil(t *testing.T) { +func TestStructedLogFieldNil(t *testing.T) { w := new(mockWriter) old := writer.Swap(w) defer writer.Store(old) - assert.Panics(t, func() { - var ps panicStringer - Infow("test", Field("bb", ps)) - }) assert.NotPanics(t, func() { var s *string Infow("test", Field("bb", s)) @@ -365,6 +361,12 @@ func TestStructedLogInfowNil(t *testing.T) { var e *nilError Errorw("test", Field("bb", e)) }) + assert.NotPanics(t, func() { + var p panicStringer + Infow("test", Field("bb", p)) + var ps innerPanicStringer + Infow("test", Field("bb", ps)) + }) } func TestStructedLogInfoConsoleAny(t *testing.T) { @@ -895,7 +897,18 @@ func (s *nilStringer) String() string { return s.Name } -type panicStringer struct{} +type innerPanicStringer struct { + Inner *struct { + Name string + } +} + +func (s innerPanicStringer) String() string { + return s.Inner.Name +} + +type panicStringer struct { +} func (s panicStringer) String() string { panic("panic")