Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: log panic on Error() or String() panics #4136

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: log panic on Error() or String() panics
  • Loading branch information
kevwan committed May 10, 2024
commit 6b536d5d4e1d01503555e79c8e3d2b671aaed09f
2 changes: 1 addition & 1 deletion core/logx/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}()
Expand Down
25 changes: 19 additions & 6 deletions core/logx/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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) {
Expand Down Expand Up @@ -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")
Expand Down