...
1
2
3
4 package test
5
6 import (
7 "io/ioutil"
8 "sync"
9
10 "github.com/sirupsen/logrus"
11 )
12
13
14 type Hook struct {
15
16
17
18 Entries []logrus.Entry
19 mu sync.RWMutex
20 }
21
22
23 func NewGlobal() *Hook {
24
25 hook := new(Hook)
26 logrus.AddHook(hook)
27
28 return hook
29
30 }
31
32
33 func NewLocal(logger *logrus.Logger) *Hook {
34
35 hook := new(Hook)
36 logger.Hooks.Add(hook)
37
38 return hook
39
40 }
41
42
43 func NewNullLogger() (*logrus.Logger, *Hook) {
44
45 logger := logrus.New()
46 logger.Out = ioutil.Discard
47
48 return logger, NewLocal(logger)
49
50 }
51
52 func (t *Hook) Fire(e *logrus.Entry) error {
53 t.mu.Lock()
54 defer t.mu.Unlock()
55 t.Entries = append(t.Entries, *e)
56 return nil
57 }
58
59 func (t *Hook) Levels() []logrus.Level {
60 return logrus.AllLevels
61 }
62
63
64 func (t *Hook) LastEntry() *logrus.Entry {
65 t.mu.RLock()
66 defer t.mu.RUnlock()
67 i := len(t.Entries) - 1
68 if i < 0 {
69 return nil
70 }
71 return &t.Entries[i]
72 }
73
74
75 func (t *Hook) AllEntries() []*logrus.Entry {
76 t.mu.RLock()
77 defer t.mu.RUnlock()
78
79 entries := make([]*logrus.Entry, len(t.Entries))
80 for i := 0; i < len(t.Entries); i++ {
81
82 entries[i] = &t.Entries[i]
83 }
84 return entries
85 }
86
87
88 func (t *Hook) Reset() {
89 t.mu.Lock()
90 defer t.mu.Unlock()
91 t.Entries = make([]logrus.Entry, 0)
92 }
93
View as plain text