forked from mirror/logrus
hooks: don't skip entire chain if a hook fails
This commit is contained in:
parent
080ca65fb5
commit
af41a51636
|
@ -1,3 +1,7 @@
|
|||
# 0.11.2
|
||||
|
||||
* bug: don't skip hooks on failure (#474)
|
||||
|
||||
# 0.11.1
|
||||
|
||||
* bug: fix tty detection with custom out (#471)
|
||||
|
|
2
entry.go
2
entry.go
|
@ -95,7 +95,7 @@ func (entry Entry) log(level Level, msg string) {
|
|||
|
||||
if err := entry.Logger.Hooks.Fire(level, &entry); err != nil {
|
||||
entry.Logger.mu.Lock()
|
||||
fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err)
|
||||
fmt.Fprintf(os.Stderr, "Failed to fire hook(s): %v\n", err)
|
||||
entry.Logger.mu.Unlock()
|
||||
}
|
||||
buffer = bufferPool.Get().(*bytes.Buffer)
|
||||
|
|
22
hook_test.go
22
hook_test.go
|
@ -1,6 +1,7 @@
|
|||
package logrus
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -120,3 +121,24 @@ func TestErrorHookShouldFireOnError(t *testing.T) {
|
|||
assert.Equal(t, hook.Fired, true)
|
||||
})
|
||||
}
|
||||
|
||||
type FailingHook struct {
|
||||
TestHook
|
||||
}
|
||||
|
||||
func (hook *FailingHook) Fire(entry *Entry) error {
|
||||
return errors.New("sad walrus")
|
||||
}
|
||||
|
||||
func TestHookShouldFireAfterFailureHook(t *testing.T) {
|
||||
failureHook := new(FailingHook)
|
||||
hook := new(TestHook)
|
||||
|
||||
LogAndAssertJSON(t, func(log *Logger) {
|
||||
log.Hooks.Add(failureHook)
|
||||
log.Hooks.Add(hook)
|
||||
log.Error("test")
|
||||
}, func(fields Fields) {
|
||||
assert.Equal(t, hook.Fired, true)
|
||||
})
|
||||
}
|
||||
|
|
10
hooks.go
10
hooks.go
|
@ -1,5 +1,9 @@
|
|||
package logrus
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// A hook to be fired when logging on the logging levels returned from
|
||||
// `Levels()` on your implementation of the interface. Note that this is not
|
||||
// fired in a goroutine or a channel with workers, you should handle such
|
||||
|
@ -24,11 +28,13 @@ func (hooks LevelHooks) Add(hook Hook) {
|
|||
// Fire all the hooks for the passed level. Used by `entry.log` to fire
|
||||
// appropriate hooks for a log entry.
|
||||
func (hooks LevelHooks) Fire(level Level, entry *Entry) error {
|
||||
var aggErr error
|
||||
|
||||
for _, hook := range hooks[level] {
|
||||
if err := hook.Fire(entry); err != nil {
|
||||
return err
|
||||
aggErr = fmt.Errorf("%s | %s", aggErr, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return aggErr
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue