Skip to content

Commit

Permalink
when passing a single, bare value to logger functions…
Browse files Browse the repository at this point in the history
if the bare value is an error, use the key “error”
  • Loading branch information
ansel1 committed Oct 3, 2019
1 parent b14ad8c commit c5b1037
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
6 changes: 4 additions & 2 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ func (l *Core) sweetenFields(args []interface{}) []zap.Field {
if len(args) == 1 {
// passed a bare arg with no key. We'll handle this
// as a special case
fields = append(fields, zap.Any("", args[0]))
return fields
if err, ok := args[0].(error); ok {
return append(fields, zap.Error(err))
}
return append(fields, zap.Any("", args[0]))
}

// Make sure this element isn't a dangling key.
Expand Down
22 changes: 22 additions & 0 deletions core_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package flume

import (
"errors"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"testing"
)

func TestSweetenFields(t *testing.T) {

// single value, instead of key/value pairs
c := NewCore("asdf")
fields := c.sweetenFields([]interface{}{1})

assert.Equal(t, []zap.Field{zap.Int("", 1)}, fields)

// if the bare value is an error, use the key "error"
err := errors.New("blue")
fields = c.sweetenFields([]interface{}{err})
assert.Equal(t, []zap.Field{zap.NamedError("error", err)}, fields)
}

0 comments on commit c5b1037

Please sign in to comment.