From c5b1037a5973b1d2977486254ce286fb77ea3db6 Mon Sep 17 00:00:00 2001 From: Russ Egan Date: Thu, 3 Oct 2019 13:38:30 -0500 Subject: [PATCH] =?UTF-8?q?when=20passing=20a=20single,=20bare=20value=20t?= =?UTF-8?q?o=20logger=20functions=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit if the bare value is an error, use the key “error” --- core.go | 6 ++++-- core_test.go | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 core_test.go diff --git a/core.go b/core.go index df2a7e2..52e3fc5 100644 --- a/core.go +++ b/core.go @@ -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. diff --git a/core_test.go b/core_test.go new file mode 100644 index 0000000..6bbd3ab --- /dev/null +++ b/core_test.go @@ -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) +}