forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter.go
56 lines (49 loc) · 1.45 KB
/
formatter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package spy
import (
"sync"
"github.com/sirupsen/logrus"
)
// EntryAssertFormatter is a log formatter, which gather all logged entries
type EntryAssertFormatter struct {
entries []logrus.Entry
Underlying logrus.Formatter
mu sync.RWMutex
}
// Format appends each entry to entries slice
func (f *EntryAssertFormatter) Format(entry *logrus.Entry) ([]byte, error) {
f.appendThreadSafe(*entry)
return f.Underlying.Format(entry)
}
// AnyMatches iterates over all stored entries and execute given matcher on it.
// Return true if any entries was successful matched
func (f *EntryAssertFormatter) AnyMatches(matcher func(entry logrus.Entry) bool) bool {
f.mu.RLock()
copyOfEntries := make([]logrus.Entry, len(f.entries))
copy(copyOfEntries, f.entries)
f.mu.RUnlock()
for _, entry := range copyOfEntries {
if matcher(entry) {
return true
}
}
return false
}
// AllEntriesMatches iterates over all stored entries and execute given matcher on it.
// Return true only if all entries was successful matched
func (f *EntryAssertFormatter) AllEntriesMatches(matcher func(entry logrus.Entry) bool) bool {
f.mu.RLock()
copyOfEntries := make([]logrus.Entry, len(f.entries))
copy(copyOfEntries, f.entries)
f.mu.RUnlock()
for _, entry := range copyOfEntries {
if !matcher(entry) {
return false
}
}
return true
}
func (f *EntryAssertFormatter) appendThreadSafe(entry logrus.Entry) {
f.mu.Lock()
defer f.mu.Unlock()
f.entries = append(f.entries, entry)
}