forked from mirror/logrus
Added WithError(err).
This commit is contained in:
parent
52919f182f
commit
c24d0555d7
8
entry.go
8
entry.go
|
@ -8,6 +8,9 @@ import (
|
||||||
"time"
|
"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
|
// 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,
|
// 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
|
// 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
|
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.
|
// Add a single field to the Entry.
|
||||||
func (entry *Entry) WithField(key string, value interface{}) *Entry {
|
func (entry *Entry) WithField(key string, value interface{}) *Entry {
|
||||||
return entry.WithFields(Fields{key: value})
|
return entry.WithFields(Fields{key: value})
|
||||||
|
|
|
@ -8,6 +8,21 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"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) {
|
func TestEntryPanicln(t *testing.T) {
|
||||||
errBoom := fmt.Errorf("boom time")
|
errBoom := fmt.Errorf("boom time")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue