Added WithError(err).

This commit is contained in:
Joern Barthel 2015-05-13 13:35:03 +02:00
parent 52919f182f
commit c24d0555d7
2 changed files with 23 additions and 0 deletions

View File

@ -8,6 +8,9 @@ import (
"time"
)
// Defines the key when adding error using WithError.
var ErrorKey = "error"
// An entry is the final or intermediate Logrus logging entry. It contains all
// the fields passed with WithField{,s}. It's finally logged when Debug, Info,
// Warn, Error, Fatal or Panic is called on it. These objects can be reused and
@ -53,6 +56,11 @@ func (entry *Entry) String() (string, error) {
return reader.String(), err
}
// Add an error as single field (with key "error") to the Entry.
func (entry *Entry) WithError(err error) *Entry {
return entry.WithField(ErrorKey, err)
}
// Add a single field to the Entry.
func (entry *Entry) WithField(key string, value interface{}) *Entry {
return entry.WithFields(Fields{key: value})

View File

@ -8,6 +8,21 @@ import (
"github.com/stretchr/testify/assert"
)
func TestEntryWithError(t *testing.T) {
err := fmt.Errorf("kaboom at layer %d", 4711)
logger := New()
logger.Out = &bytes.Buffer{}
entry := NewEntry(logger)
assert.Equal(t, err, entry.WithError(err).Data["error"])
ErrorKey = "err"
assert.Equal(t, err, entry.WithError(err).Data["err"])
}
func TestEntryPanicln(t *testing.T) {
errBoom := fmt.Errorf("boom time")