-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_throttle.go
179 lines (145 loc) · 3.7 KB
/
log_throttle.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package log
import (
"context"
"sync"
"sync/atomic"
"time"
)
const (
DebugLevel = iota
InfoLevel
WarnLevel
ErrorLevel
PanicLevel
)
type ThrottleLogger struct {
logger Logger
interval time.Duration
logs *sync.Map
wg *sync.WaitGroup
closeCh, closedCh chan struct{}
}
type throttleKey struct {
level int
msg string
}
type throttleLog struct {
ctx context.Context
count *int32
keysAndValues []interface{}
}
func Throttle(logger Logger, interval time.Duration) (*ThrottleLogger, chan struct{}) {
closeCh := make(chan struct{})
closedCh := make(chan struct{})
wg := new(sync.WaitGroup)
t := &ThrottleLogger{
logger: logger,
interval: interval,
logs: &sync.Map{},
wg: wg,
closeCh: closeCh,
closedCh: closedCh,
}
wg.Add(1)
go func() {
defer wg.Done()
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
t.Flush()
case <-closeCh:
return
}
}
}()
return t, closedCh
}
func (t *ThrottleLogger) Stop() {
t.close()
}
func (t *ThrottleLogger) Flush() {
t.logs.Range(func(key, value interface{}) bool {
tKey := key.(throttleKey)
tLog := value.(*throttleLog)
if tLog == nil {
return true
}
logCount := atomic.LoadInt32(tLog.count)
if logCount == 0 {
t.logs.Delete(key)
return true
}
keysAndValues := tLog.keysAndValues
if logCount > 1 {
keysAndValues = append(keysAndValues, "times", logCount)
}
t.logMessage(tLog.ctx, tKey.level, tKey.msg, keysAndValues...)
atomic.AddInt32(tLog.count, -logCount)
return true
})
}
func (t *ThrottleLogger) Debug(ctx context.Context, msg string, keysAndValues ...interface{}) {
t.throttleLogMessage(ctx, DebugLevel, msg, keysAndValues...)
}
func (t *ThrottleLogger) Info(ctx context.Context, msg string, keysAndValues ...interface{}) {
t.throttleLogMessage(ctx, InfoLevel, msg, keysAndValues...)
}
func (t *ThrottleLogger) Warn(ctx context.Context, msg string, keysAndValues ...interface{}) {
t.throttleLogMessage(ctx, WarnLevel, msg, keysAndValues...)
}
func (t *ThrottleLogger) Error(ctx context.Context, msg string, keysAndValues ...interface{}) {
t.throttleLogMessage(ctx, ErrorLevel, msg, keysAndValues...)
}
func (t *ThrottleLogger) Panic(ctx context.Context, msg string, keysAndValues ...interface{}) {
t.throttleLogMessage(ctx, PanicLevel, msg, keysAndValues...)
}
func (t *ThrottleLogger) IsDebug(ctx context.Context) bool {
return t.logger.IsDebug(ctx)
}
// Dump should be used only during development and should not stay in production code.
func (t *ThrottleLogger) Dump(msg string, v ...interface{}) {
t.logger.Dump(msg, v...)
}
func (t *ThrottleLogger) throttleLogMessage(ctx context.Context, logLevel int, msg string, keysAndValues ...interface{}) {
log, ok := t.logs.LoadOrStore(throttleKey{level: logLevel, msg: msg}, &throttleLog{ctx: ctx, count: new(int32), keysAndValues: keysAndValues})
if !ok {
// first time, call log
t.logMessage(ctx, logLevel, msg, keysAndValues...)
} else {
atomic.AddInt32(log.(*throttleLog).count, 1)
}
}
func (t *ThrottleLogger) logMessage(ctx context.Context, logLevel int, msg string, keysAndValues ...interface{}) {
var logFn func(context.Context, string, ...interface{})
switch logLevel {
case DebugLevel:
logFn = t.logger.Debug
case InfoLevel:
logFn = t.logger.Info
case WarnLevel:
logFn = t.logger.Warn
case ErrorLevel:
logFn = t.logger.Error
case PanicLevel:
logFn = t.logger.Panic
default:
return
}
logFn(ctx, msg, keysAndValues...)
}
func (t *ThrottleLogger) close() {
defer func() {
t.wg.Wait()
if t.closedCh != nil {
close(t.closedCh)
t.closedCh = nil
}
}()
if t.closeCh == nil {
return
}
close(t.closeCh)
t.closeCh = nil
}