Skip to content

Commit

Permalink
Add AnErr field type
Browse files Browse the repository at this point in the history
  • Loading branch information
rs committed Jun 2, 2017
1 parent 67803eb commit 49d553c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
6 changes: 6 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ func (c Context) Str(key, val string) Context {
return c
}

// AnErr adds the field key with err as a string to the logger context.
func (c Context) AnErr(key string, err error) Context {
c.l.context = appendErrorKey(c.l.context, key, err)
return c
}

// Err adds the field "error" with err as a string to the logger context.
// To customize the key name, change zerolog.ErrorFieldName.
func (c Context) Err(err error) Context {
Expand Down
11 changes: 11 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,18 @@ func (e *Event) Str(key, val string) *Event {
return e
}

// AnErr adds the field key with err as a string to the *Event context.
// If err is nil, no field is added.
func (e *Event) AnErr(key string, err error) *Event {
if !e.enabled {
return e
}
e.buf = appendErrorKey(e.buf, key, err)
return e
}

// Err adds the field "error" with err as a string to the *Event context.
// If err is nil, no field is added.
// To customize the key name, change zerolog.ErrorFieldName.
func (e *Event) Err(err error) *Event {
if !e.enabled {
Expand Down
9 changes: 8 additions & 1 deletion field.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ func appendString(dst []byte, key, val string) []byte {
return appendJSONString(appendKey(dst, key), val)
}

func appendErrorKey(dst []byte, key string, err error) []byte {
if err == nil {
return dst
}
return appendJSONString(appendKey(dst, key), err.Error())
}

func appendError(dst []byte, err error) []byte {
return appendJSONString(appendKey(dst, ErrorFieldName), err.Error())
return appendErrorKey(dst, ErrorFieldName, err)
}

func appendBool(dst []byte, key string, val bool) []byte {
Expand Down
3 changes: 3 additions & 0 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func TestWith(t *testing.T) {
out := &bytes.Buffer{}
log := New(out).With().
Str("foo", "bar").
AnErr("some_err", nil).
Err(errors.New("some error")).
Bool("bool", true).
Int("int", 1).
Expand Down Expand Up @@ -103,6 +104,7 @@ func TestFields(t *testing.T) {
log := New(out)
log.Log().
Str("foo", "bar").
AnErr("some_err", nil).
Err(errors.New("some error")).
Bool("bool", true).
Int("int", 1).
Expand All @@ -129,6 +131,7 @@ func TestFieldsDisabled(t *testing.T) {
log := New(out).Level(InfoLevel)
log.Debug().
Str("foo", "bar").
AnErr("some_err", nil).
Err(errors.New("some error")).
Bool("bool", true).
Int("int", 1).
Expand Down

0 comments on commit 49d553c

Please sign in to comment.