Skip to content

Commit c5b1037

Browse files
committed
when passing a single, bare value to logger functions…
if the bare value is an error, use the key “error”
1 parent b14ad8c commit c5b1037

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

core.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,10 @@ func (l *Core) sweetenFields(args []interface{}) []zap.Field {
132132
if len(args) == 1 {
133133
// passed a bare arg with no key. We'll handle this
134134
// as a special case
135-
fields = append(fields, zap.Any("", args[0]))
136-
return fields
135+
if err, ok := args[0].(error); ok {
136+
return append(fields, zap.Error(err))
137+
}
138+
return append(fields, zap.Any("", args[0]))
137139
}
138140

139141
// Make sure this element isn't a dangling key.

core_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package flume
2+
3+
import (
4+
"errors"
5+
"github.com/stretchr/testify/assert"
6+
"go.uber.org/zap"
7+
"testing"
8+
)
9+
10+
func TestSweetenFields(t *testing.T) {
11+
12+
// single value, instead of key/value pairs
13+
c := NewCore("asdf")
14+
fields := c.sweetenFields([]interface{}{1})
15+
16+
assert.Equal(t, []zap.Field{zap.Int("", 1)}, fields)
17+
18+
// if the bare value is an error, use the key "error"
19+
err := errors.New("blue")
20+
fields = c.sweetenFields([]interface{}{err})
21+
assert.Equal(t, []zap.Field{zap.NamedError("error", err)}, fields)
22+
}

0 commit comments

Comments
 (0)