6 "github.com/Sirupsen/logrus"
9 // test.Hook is a hook designed for dealing with logs in test scenarios.
11 Entries []*logrus.Entry
14 // Installs a test hook for the global logger.
15 func NewGlobal() *Hook {
24 // Installs a test hook for a given local logger.
25 func NewLocal(logger *logrus.Logger) *Hook {
28 logger.Hooks.Add(hook)
34 // Creates a discarding logger and installs the test hook.
35 func NewNullLogger() (*logrus.Logger, *Hook) {
37 logger := logrus.New()
38 logger.Out = ioutil.Discard
40 return logger, NewLocal(logger)
44 func (t *Hook) Fire(e *logrus.Entry) error {
45 t.Entries = append(t.Entries, e)
49 func (t *Hook) Levels() []logrus.Level {
50 return logrus.AllLevels
53 // LastEntry returns the last entry that was logged or nil.
54 func (t *Hook) LastEntry() (l *logrus.Entry) {
56 if i := len(t.Entries) - 1; i < 0 {
64 // Reset removes all Entries from this test hook.
65 func (t *Hook) Reset() {
66 t.Entries = make([]*logrus.Entry, 0)