-
Notifications
You must be signed in to change notification settings - Fork 18
/
event_logger.go
164 lines (138 loc) · 3.09 KB
/
event_logger.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package astiencoder
import (
"context"
"fmt"
"sync"
"time"
"github.com/asticode/go-astikit"
)
type EventLogger struct {
cancel context.CancelFunc
ctx context.Context
is map[string]*eventLoggerItem // Indexed by key
l astikit.CompleteLogger
m *sync.Mutex // Locks p
messageMergingPeriod time.Duration
}
type eventLoggerItem struct {
count int
createdAt time.Time
key string
ll astikit.LoggerLevel
msg string
}
func newEventLoggerItem(ll astikit.LoggerLevel, key, msg string) *eventLoggerItem {
return &eventLoggerItem{
createdAt: time.Now(),
key: key,
ll: ll,
msg: msg,
}
}
func MessageMergingEventHandlerLogAdapter(period time.Duration) EventHandlerLogAdapter {
return func(_ *EventHandler, l *EventLogger) {
l.messageMergingPeriod = period
}
}
func newEventLogger(i astikit.StdLogger) *EventLogger {
return &EventLogger{
is: make(map[string]*eventLoggerItem),
l: astikit.AdaptStdLogger(i),
m: &sync.Mutex{},
}
}
func (l *EventLogger) Start(ctx context.Context) *EventLogger {
// Create context
l.ctx, l.cancel = context.WithCancel(ctx)
// No need to start anything
if l.messageMergingPeriod == 0 {
return l
}
// Execute in a goroutine since this is blocking
go func() {
// Create ticker
t := time.NewTicker(200 * time.Millisecond)
defer t.Stop()
// Loop
for {
select {
case <-t.C:
l.tick()
case <-l.ctx.Done():
return
}
}
}()
return l
}
func (l *EventLogger) Close() {
if l.cancel != nil {
l.cancel()
}
l.purge()
}
func (l *EventLogger) tick() {
// Lock
l.m.Lock()
defer l.m.Unlock()
// Get now
n := time.Now()
// Loop through items
for k, i := range l.is {
// Period has been reached
if n.Sub(i.createdAt) > l.messageMergingPeriod {
l.dumpItem(k, i)
}
}
}
func (l *EventLogger) purge() {
// Lock
l.m.Lock()
defer l.m.Unlock()
// Loop through items
for k, i := range l.is {
l.dumpItem(k, i)
}
}
func (l *EventLogger) dumpItem(k string, i *eventLoggerItem) {
if i.count > 1 {
l.l.Write(i.ll, fmt.Sprintf("astiencoder: pattern repeated %d times: %s", i.count, i.key))
} else if i.count == 1 {
l.l.Write(i.ll, "astiencoder: pattern repeated once: "+i.msg)
}
delete(l.is, k)
}
func (l *EventLogger) process(ll astikit.LoggerLevel, key, msg string) {
// Merge messages
if l.messageMergingPeriod > 0 {
// Merge
if stop := l.merge(ll, key, msg); stop {
return
}
}
// Write
l.l.Write(ll, msg)
}
func (l *EventLogger) merge(ll astikit.LoggerLevel, key, msg string) (stop bool) {
// Lock
l.m.Lock()
defer l.m.Unlock()
// Create final key
k := ll.String() + ":" + key
// Check whether item exists
i, ok := l.is[k]
if ok {
i.count++
return true
}
// Create item
l.is[k] = newEventLoggerItem(ll, key, msg)
return false
}
func (l *EventLogger) Writek(ll astikit.LoggerLevel, key, msg string) {
l.process(ll, key, msg)
}
func (l *EventLogger) Writef(ll astikit.LoggerLevel, format string, v ...interface{}) {
msg := fmt.Sprintf(format, v...)
l.process(ll, msg, msg)
}