Add AnErr field type

This commit is contained in:
Olivier Poitrey 2017-06-02 00:56:14 -07:00
parent 67803eb791
commit 49d553c9b8
4 changed files with 28 additions and 1 deletions

View File

@ -26,6 +26,12 @@ func (c Context) Str(key, val string) Context {
return c 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. // Err adds the field "error" with err as a string to the logger context.
// To customize the key name, change zerolog.ErrorFieldName. // To customize the key name, change zerolog.ErrorFieldName.
func (c Context) Err(err error) Context { func (c Context) Err(err error) Context {

View File

@ -116,7 +116,18 @@ func (e *Event) Str(key, val string) *Event {
return e 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. // 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. // To customize the key name, change zerolog.ErrorFieldName.
func (e *Event) Err(err error) *Event { func (e *Event) Err(err error) *Event {
if !e.enabled { if !e.enabled {

View File

@ -19,8 +19,15 @@ func appendString(dst []byte, key, val string) []byte {
return appendJSONString(appendKey(dst, key), val) 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 { 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 { func appendBool(dst []byte, key string, val bool) []byte {

View File

@ -76,6 +76,7 @@ func TestWith(t *testing.T) {
out := &bytes.Buffer{} out := &bytes.Buffer{}
log := New(out).With(). log := New(out).With().
Str("foo", "bar"). Str("foo", "bar").
AnErr("some_err", nil).
Err(errors.New("some error")). Err(errors.New("some error")).
Bool("bool", true). Bool("bool", true).
Int("int", 1). Int("int", 1).
@ -103,6 +104,7 @@ func TestFields(t *testing.T) {
log := New(out) log := New(out)
log.Log(). log.Log().
Str("foo", "bar"). Str("foo", "bar").
AnErr("some_err", nil).
Err(errors.New("some error")). Err(errors.New("some error")).
Bool("bool", true). Bool("bool", true).
Int("int", 1). Int("int", 1).
@ -129,6 +131,7 @@ func TestFieldsDisabled(t *testing.T) {
log := New(out).Level(InfoLevel) log := New(out).Level(InfoLevel)
log.Debug(). log.Debug().
Str("foo", "bar"). Str("foo", "bar").
AnErr("some_err", nil).
Err(errors.New("some error")). Err(errors.New("some error")).
Bool("bool", true). Bool("bool", true).
Int("int", 1). Int("int", 1).