@@ -28,56 +28,57 @@ import (
28
28
"github.com/sirupsen/logrus"
29
29
)
30
30
31
- // CacheLogrusHook implements the logrus.Hook interface and could be used to check
31
+ // LogCache implements the logrus.Hook interface and could be used to check
32
32
// if log messages were outputted
33
- type CacheLogrusHook struct {
33
+ type LogCache struct {
34
34
HookedLevels []logrus.Level
35
35
mutex sync.RWMutex
36
36
messageCache []logrus.Entry
37
37
}
38
38
39
39
// Levels just returns whatever was stored in the HookedLevels slice
40
- func (clh * CacheLogrusHook ) Levels () []logrus.Level {
41
- return clh .HookedLevels
40
+ func (lc * LogCache ) Levels () []logrus.Level {
41
+ return lc .HookedLevels
42
42
}
43
43
44
44
// Fire saves whatever message the logrus library passed in the cache
45
- func (clh * CacheLogrusHook ) Fire (e * logrus.Entry ) error {
46
- clh .mutex .Lock ()
47
- defer clh .mutex .Unlock ()
48
- clh .messageCache = append (clh .messageCache , * e )
45
+ func (lc * LogCache ) Fire (e * logrus.Entry ) error {
46
+ lc .mutex .Lock ()
47
+ defer lc .mutex .Unlock ()
48
+ lc .messageCache = append (lc .messageCache , * e )
49
49
return nil
50
50
}
51
51
52
52
// Drain returns the currently stored messages and deletes them from the cache
53
- func (clh * CacheLogrusHook ) Drain () []logrus.Entry {
54
- clh .mutex .Lock ()
55
- defer clh .mutex .Unlock ()
56
- res := clh .messageCache
57
- clh .messageCache = []logrus.Entry {}
53
+ func (lc * LogCache ) Drain () []logrus.Entry {
54
+ lc .mutex .Lock ()
55
+ defer lc .mutex .Unlock ()
56
+ res := lc .messageCache
57
+ lc .messageCache = []logrus.Entry {}
58
58
return res
59
59
}
60
60
61
61
// Contains returns true if msg is contained in any of the cached logged events
62
62
// or false otherwise.
63
- func (clh * CacheLogrusHook ) Contains (msg string ) bool {
64
- clh .mutex .RLock ()
65
- defer clh .mutex .RUnlock ()
66
- for _ , evt := range clh .messageCache {
63
+ func (lc * LogCache ) Contains (msg string ) bool {
64
+ lc .mutex .RLock ()
65
+ defer lc .mutex .RUnlock ()
66
+ for _ , evt := range lc .messageCache {
67
67
if strings .Contains (evt .Message , msg ) {
68
68
return true
69
69
}
70
70
}
71
71
return false
72
72
}
73
73
74
- var _ logrus.Hook = & CacheLogrusHook {}
74
+ var _ logrus.Hook = & LogCache {}
75
75
76
- // LogHook sets logger to DebugLevel, attaches a CacheLogrusHook and returns it.
77
- func LogHook (logger * logrus.Logger ) * CacheLogrusHook {
78
- logHook := & CacheLogrusHook {HookedLevels : []logrus.Level {logrus .DebugLevel , logrus .WarnLevel }}
76
+ // AttachLogCache sets logger to DebugLevel, attaches a LogCache hook and
77
+ // returns it.
78
+ func AttachLogCache (logger * logrus.Logger ) * LogCache {
79
+ lc := & LogCache {HookedLevels : []logrus.Level {logrus .DebugLevel , logrus .WarnLevel }}
79
80
logger .SetLevel (logrus .DebugLevel )
80
- logger .AddHook (logHook )
81
+ logger .AddHook (lc )
81
82
logger .SetOutput (ioutil .Discard )
82
- return logHook
83
+ return lc
83
84
}
0 commit comments