Skip to content

Commit

Permalink
SugaredLogger/sweetenFields: Don't panic (#949)
Browse files Browse the repository at this point in the history
SugaredLogger should not cause panics in user code.

For keys without matching values or non-string keys, instead of
panicking, log an error and move on.

Resolves #896
  • Loading branch information
atrn0 committed Jun 7, 2021
1 parent 4950e39 commit 084fb2a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions sugar.go
Expand Up @@ -266,7 +266,7 @@ func (s *SugaredLogger) sweetenFields(args []interface{}) []Field {

// Make sure this element isn't a dangling key.
if i == len(args)-1 {
s.base.DPanic(_oddNumberErrMsg, Any("ignored", args[i]))
s.base.Error(_oddNumberErrMsg, Any("ignored", args[i]))
break
}

Expand All @@ -287,7 +287,7 @@ func (s *SugaredLogger) sweetenFields(args []interface{}) []Field {

// If we encountered any invalid key-value pairs, log an error.
if len(invalid) > 0 {
s.base.DPanic(_nonStringKeyErrMsg, Array("invalid", invalid))
s.base.Error(_nonStringKeyErrMsg, Array("invalid", invalid))
}
return fields
}
Expand Down
4 changes: 2 additions & 2 deletions sugar_test.go
Expand Up @@ -36,13 +36,13 @@ func TestSugarWith(t *testing.T) {
// Convenience functions to create expected error logs.
ignored := func(msg interface{}) observer.LoggedEntry {
return observer.LoggedEntry{
Entry: zapcore.Entry{Level: DPanicLevel, Message: _oddNumberErrMsg},
Entry: zapcore.Entry{Level: ErrorLevel, Message: _oddNumberErrMsg},
Context: []Field{Any("ignored", msg)},
}
}
nonString := func(pairs ...invalidPair) observer.LoggedEntry {
return observer.LoggedEntry{
Entry: zapcore.Entry{Level: DPanicLevel, Message: _nonStringKeyErrMsg},
Entry: zapcore.Entry{Level: ErrorLevel, Message: _nonStringKeyErrMsg},
Context: []Field{Array("invalid", invalidPairs(pairs))},
}
}
Expand Down

0 comments on commit 084fb2a

Please sign in to comment.