提交 af41a516 编写于 作者: S Simon Eskildsen

hooks: don't skip entire chain if a hook fails

上级 080ca65f
# 0.11.2
* bug: don't skip hooks on failure (#474)
# 0.11.1
* bug: fix tty detection with custom out (#471)
......
......@@ -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)
......
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)
})
}
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
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册