Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 9c5aaab

Browse files
author
Ivan Mirić
committed
Rename CacheLogrusHook to LogCache
Resolves #130 (comment)
1 parent 1b28e42 commit 9c5aaab

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

tests/network_manager_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ func TestDataURLSkipRequest(t *testing.T) {
3737
bt.Browser.Close()
3838
})
3939

40-
logHook := testutils.LogHook(bt.State.Logger)
40+
lc := testutils.AttachLogCache(bt.State.Logger)
4141

4242
p.Goto("data:text/html,hello", nil)
4343

44-
assert.True(t, logHook.Contains("skipped request handling of data URL"))
44+
assert.True(t, lc.Contains("skipped request handling of data URL"))
4545
}

testutils/logrus_hook.go

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,56 +28,57 @@ import (
2828
"github.com/sirupsen/logrus"
2929
)
3030

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
3232
// if log messages were outputted
33-
type CacheLogrusHook struct {
33+
type LogCache struct {
3434
HookedLevels []logrus.Level
3535
mutex sync.RWMutex
3636
messageCache []logrus.Entry
3737
}
3838

3939
// 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
4242
}
4343

4444
// 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)
4949
return nil
5050
}
5151

5252
// 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{}
5858
return res
5959
}
6060

6161
// Contains returns true if msg is contained in any of the cached logged events
6262
// 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 {
6767
if strings.Contains(evt.Message, msg) {
6868
return true
6969
}
7070
}
7171
return false
7272
}
7373

74-
var _ logrus.Hook = &CacheLogrusHook{}
74+
var _ logrus.Hook = &LogCache{}
7575

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}}
7980
logger.SetLevel(logrus.DebugLevel)
80-
logger.AddHook(logHook)
81+
logger.AddHook(lc)
8182
logger.SetOutput(ioutil.Discard)
82-
return logHook
83+
return lc
8384
}

0 commit comments

Comments
 (0)