From c24d0555d76f039b3dec1c3397979ba373647ec4 Mon Sep 17 00:00:00 2001 From: Joern Barthel Date: Wed, 13 May 2015 13:35:03 +0200 Subject: [PATCH 1/3] Added WithError(err). --- entry.go | 8 ++++++++ entry_test.go | 15 +++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/entry.go b/entry.go index 17fe6f7..b9206de 100644 --- a/entry.go +++ b/entry.go @@ -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}) diff --git a/entry_test.go b/entry_test.go index 98717df..a824e64 100644 --- a/entry_test.go +++ b/entry_test.go @@ -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") From e3e5de11c4ffc79a6da0d7c6c4f73f0bff74a077 Mon Sep 17 00:00:00 2001 From: Joern Barthel Date: Tue, 19 May 2015 19:50:55 +0200 Subject: [PATCH 2/3] Implement WithError(err) in exported, fixed doco. --- entry.go | 4 ++-- entry_test.go | 13 +++++++++++-- exported.go | 5 +++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/entry.go b/entry.go index b9206de..c0f398f 100644 --- a/entry.go +++ b/entry.go @@ -8,7 +8,7 @@ import ( "time" ) -// Defines the key when adding error using WithError. +// Defines the key when adding errors using WithError. var ErrorKey = "error" // An entry is the final or intermediate Logrus logging entry. It contains all @@ -56,7 +56,7 @@ func (entry *Entry) String() (string, error) { return reader.String(), err } -// Add an error as single field (with key "error") to the Entry. +// Add an error as single field (using the key defined in ErrorKey) to the Entry. func (entry *Entry) WithError(err error) *Entry { return entry.WithField(ErrorKey, err) } diff --git a/entry_test.go b/entry_test.go index a824e64..99c3b41 100644 --- a/entry_test.go +++ b/entry_test.go @@ -10,16 +10,25 @@ import ( func TestEntryWithError(t *testing.T) { + assert := assert.New(t) + + defer func() { + ErrorKey = "error" + }() + err := fmt.Errorf("kaboom at layer %d", 4711) + assert.Equal(err, WithError(err).Data["error"]) + logger := New() logger.Out = &bytes.Buffer{} entry := NewEntry(logger) - assert.Equal(t, err, entry.WithError(err).Data["error"]) + assert.Equal(err, entry.WithError(err).Data["error"]) ErrorKey = "err" - assert.Equal(t, err, entry.WithError(err).Data["err"]) + + assert.Equal(err, entry.WithError(err).Data["err"]) } diff --git a/exported.go b/exported.go index a67e1b8..9a0120a 100644 --- a/exported.go +++ b/exported.go @@ -48,6 +48,11 @@ func AddHook(hook Hook) { std.Hooks.Add(hook) } +// WithError creates an entry from the standard logger and adds an error to it, using the value defined in ErrorKey as key. +func WithError(err error) *Entry { + return std.WithField(ErrorKey, err) +} + // WithField creates an entry from the standard logger and adds a field to // it. If you want multiple fields, use `WithFields`. // From 756db3cd2dd0d5a461aeefd25cbe5223d4f06d9f Mon Sep 17 00:00:00 2001 From: Joern Barthel Date: Tue, 19 May 2015 20:20:59 +0200 Subject: [PATCH 3/3] Implement casting of *Entry to error. --- entry.go | 68 +++++++++++++++++++++++++++++++++------------------ entry_test.go | 19 ++++++++++++-- 2 files changed, 61 insertions(+), 26 deletions(-) diff --git a/entry.go b/entry.go index c0f398f..b8889f4 100644 --- a/entry.go +++ b/entry.go @@ -56,6 +56,14 @@ func (entry *Entry) String() (string, error) { return reader.String(), err } +// ToError returns the field value of ErrorKey (nil) +func (entry *Entry) ToError() error { + if err, ok := entry.Data[ErrorKey].(error); ok { + return err + } + return nil +} + // Add an error as single field (using the key defined in ErrorKey) to the Entry. func (entry *Entry) WithError(err error) *Entry { return entry.WithField(ErrorKey, err) @@ -112,36 +120,40 @@ func (entry *Entry) log(level Level, msg string) { } } -func (entry *Entry) Debug(args ...interface{}) { +func (entry *Entry) Debug(args ...interface{}) *Entry { if entry.Logger.Level >= DebugLevel { entry.log(DebugLevel, fmt.Sprint(args...)) } + return entry } -func (entry *Entry) Print(args ...interface{}) { - entry.Info(args...) +func (entry *Entry) Print(args ...interface{}) *Entry { + return entry.Info(args...) } -func (entry *Entry) Info(args ...interface{}) { +func (entry *Entry) Info(args ...interface{}) *Entry { if entry.Logger.Level >= InfoLevel { entry.log(InfoLevel, fmt.Sprint(args...)) } + return entry } -func (entry *Entry) Warn(args ...interface{}) { +func (entry *Entry) Warn(args ...interface{}) *Entry { if entry.Logger.Level >= WarnLevel { entry.log(WarnLevel, fmt.Sprint(args...)) } + return entry } -func (entry *Entry) Warning(args ...interface{}) { - entry.Warn(args...) +func (entry *Entry) Warning(args ...interface{}) *Entry { + return entry.Warn(args...) } -func (entry *Entry) Error(args ...interface{}) { +func (entry *Entry) Error(args ...interface{}) *Entry { if entry.Logger.Level >= ErrorLevel { entry.log(ErrorLevel, fmt.Sprint(args...)) } + return entry } func (entry *Entry) Fatal(args ...interface{}) { @@ -160,36 +172,40 @@ func (entry *Entry) Panic(args ...interface{}) { // Entry Printf family functions -func (entry *Entry) Debugf(format string, args ...interface{}) { +func (entry *Entry) Debugf(format string, args ...interface{}) *Entry { if entry.Logger.Level >= DebugLevel { entry.Debug(fmt.Sprintf(format, args...)) } + return entry } -func (entry *Entry) Infof(format string, args ...interface{}) { +func (entry *Entry) Infof(format string, args ...interface{}) *Entry { if entry.Logger.Level >= InfoLevel { entry.Info(fmt.Sprintf(format, args...)) } + return entry } -func (entry *Entry) Printf(format string, args ...interface{}) { - entry.Infof(format, args...) +func (entry *Entry) Printf(format string, args ...interface{}) *Entry { + return entry.Infof(format, args...) } -func (entry *Entry) Warnf(format string, args ...interface{}) { +func (entry *Entry) Warnf(format string, args ...interface{}) *Entry { if entry.Logger.Level >= WarnLevel { entry.Warn(fmt.Sprintf(format, args...)) } + return entry } -func (entry *Entry) Warningf(format string, args ...interface{}) { - entry.Warnf(format, args...) +func (entry *Entry) Warningf(format string, args ...interface{}) *Entry { + return entry.Warnf(format, args...) } -func (entry *Entry) Errorf(format string, args ...interface{}) { +func (entry *Entry) Errorf(format string, args ...interface{}) *Entry { if entry.Logger.Level >= ErrorLevel { entry.Error(fmt.Sprintf(format, args...)) } + return entry } func (entry *Entry) Fatalf(format string, args ...interface{}) { @@ -206,36 +222,40 @@ func (entry *Entry) Panicf(format string, args ...interface{}) { // Entry Println family functions -func (entry *Entry) Debugln(args ...interface{}) { +func (entry *Entry) Debugln(args ...interface{}) *Entry { if entry.Logger.Level >= DebugLevel { entry.Debug(entry.sprintlnn(args...)) } + return entry } -func (entry *Entry) Infoln(args ...interface{}) { +func (entry *Entry) Infoln(args ...interface{}) *Entry { if entry.Logger.Level >= InfoLevel { entry.Info(entry.sprintlnn(args...)) } + return entry } -func (entry *Entry) Println(args ...interface{}) { - entry.Infoln(args...) +func (entry *Entry) Println(args ...interface{}) *Entry { + return entry.Infoln(args...) } -func (entry *Entry) Warnln(args ...interface{}) { +func (entry *Entry) Warnln(args ...interface{}) *Entry { if entry.Logger.Level >= WarnLevel { entry.Warn(entry.sprintlnn(args...)) } + return entry } -func (entry *Entry) Warningln(args ...interface{}) { - entry.Warnln(args...) +func (entry *Entry) Warningln(args ...interface{}) *Entry { + return entry.Warnln(args...) } -func (entry *Entry) Errorln(args ...interface{}) { +func (entry *Entry) Errorln(args ...interface{}) *Entry { if entry.Logger.Level >= ErrorLevel { entry.Error(entry.sprintlnn(args...)) } + return entry } func (entry *Entry) Fatalln(args ...interface{}) { diff --git a/entry_test.go b/entry_test.go index 99c3b41..3473ffb 100644 --- a/entry_test.go +++ b/entry_test.go @@ -8,6 +8,23 @@ import ( "github.com/stretchr/testify/assert" ) +var err = fmt.Errorf("kaboom at layer %d", 4711) + +func TestToError(t *testing.T) { + + assert := assert.New(t) + + ctx := WithField("foo", "bar") + assert.Equal(nil, ctx.Debug("Hello").ToError()) + + ctx.Data[ErrorKey] = "error" + assert.Equal(nil, ctx.Debug("Hello").ToError()) + + ctx = ctx.WithError(err) + assert.Equal(err, ctx.Debug("Hello").ToError()) + +} + func TestEntryWithError(t *testing.T) { assert := assert.New(t) @@ -16,8 +33,6 @@ func TestEntryWithError(t *testing.T) { ErrorKey = "error" }() - err := fmt.Errorf("kaboom at layer %d", 4711) - assert.Equal(err, WithError(err).Data["error"]) logger := New()