-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmocklogger.go
242 lines (201 loc) · 8.04 KB
/
mocklogger.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package logger
import (
"io"
"sync"
"github.com/Clever/kayvee-go/v7/router"
)
// MockRouteCountLogger is a mock implementation of KayveeLogger that counts the router rules
// applied to each log call without actually formatting or writing the log line.
type MockRouteCountLogger struct {
logger KayveeLogger
routeMatches map[string][]router.RuleOutput
}
// RuleCounts returns a map of rule names to the number of times that rule has been applied
// in routing logs for MockRouteCountLogger. Only includes routing rules that have at least
// one use.
func (ml *MockRouteCountLogger) RuleCounts() map[string]int {
out := make(map[string]int)
for k, v := range ml.routeMatches {
out[k] = len(v)
}
return out
}
// RuleOutputs returns a map of rule names to the exact logs which matched that rule (after routing has
// been applied to those logs). This allows you to inspect the routed log and verify data about it.
func (ml *MockRouteCountLogger) RuleOutputs() map[string][]router.RuleOutput {
return ml.routeMatches
}
// NewMockCountLogger returns a new MockRoutCountLogger with the specified `source`.
func NewMockCountLogger(source string) *MockRouteCountLogger {
return NewMockCountLoggerWithContext(source, nil)
}
// NewMockCountLoggerWithContext returns a new MockRoutCountLogger with the specified `source` and `contextValues`.
func NewMockCountLoggerWithContext(source string, contextValues map[string]interface{}) *MockRouteCountLogger {
routeMatches := make(map[string][]router.RuleOutput)
lg := NewWithContext(source, contextValues)
lg.setFormatLogger(&routeCountingFormatLogger{
routeMatches: routeMatches,
})
mocklg := MockRouteCountLogger{
logger: lg,
routeMatches: routeMatches,
}
return &mocklg
}
/////////////////////////////
//
// routeCountingFormatLogger
//
/////////////////////////////
// routeCountingFormatLogger implements the formatLogger interface to allow for counting
// invocations of routing rules.
type routeCountingFormatLogger struct {
mu sync.Mutex
routeMatches map[string][]router.RuleOutput
}
// formatAndLog tracks routing statistics for this mock router.
// Initialization works as with the default format logger, but no formatting or logging is actually performed.
func (fl *routeCountingFormatLogger) formatAndLog(data map[string]interface{}) {
routeData, ok := data["_kvmeta"]
if !ok {
return
}
routes, ok := routeData.(map[string]interface{})["routes"]
if !ok {
return
}
for _, route := range routes.([]map[string]interface{}) {
rule := route["rule"].(string)
fl.mu.Lock()
fl.routeMatches[rule] = append(fl.routeMatches[rule], route)
fl.mu.Unlock()
}
}
// setFormatter implements the FormatLogger method.
func (fl *routeCountingFormatLogger) setFormatter(formatter Formatter) {
// we don't format anything in this mock logger
}
// setOutput implements the FormatLogger method.
func (fl *routeCountingFormatLogger) setOutput(output io.Writer) {
// we don't output anything in this mock logger
}
/////////////////////////////////////////////////////////////
//
// KayveeLogger implementation (all passthrough to logger)
//
/////////////////////////////////////////////////////////////
// SetConfig implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) SetConfig(source string, logLvl LogLevel, formatter Formatter, output io.Writer) {
ml.logger.SetConfig(source, logLvl, formatter, output)
}
// AddContext implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) AddContext(key, val string) {
ml.logger.AddContext(key, val)
}
// GetContext implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) GetContext(key string) (interface{}, bool) {
return ml.logger.GetContext(key)
}
// SetLogLevel implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) SetLogLevel(logLvl LogLevel) {
ml.logger.SetLogLevel(logLvl)
}
// SetFormatter implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) SetFormatter(formatter Formatter) {
ml.logger.SetFormatter(formatter)
}
// SetOutput implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) SetOutput(output io.Writer) {
ml.logger.SetOutput(output)
}
// setFormatLogger implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) setFormatLogger(output formatLogger) {
// Mocks need a custom format logger
}
// SetRouter implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) SetRouter(router router.Router) {
ml.logger.SetRouter(router)
}
// Trace implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) Trace(title string) {
ml.logger.Trace(title)
}
// Debug implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) Debug(title string) {
ml.logger.Debug(title)
}
// Info implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) Info(title string) {
ml.logger.Info(title)
}
// Warn implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) Warn(title string) {
ml.logger.Warn(title)
}
// Error implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) Error(title string) {
ml.logger.Error(title)
}
// Critical implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) Critical(title string) {
ml.logger.Critical(title)
}
// Counter implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) Counter(title string) {
ml.logger.Counter(title)
}
// GaugeInt implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) GaugeInt(title string, value int) {
ml.logger.GaugeInt(title, value)
}
// GaugeFloat implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) GaugeFloat(title string, value float64) {
ml.logger.GaugeFloat(title, value)
}
// Timer implements the method for the KayveeLogger interface.
// Returns Timer structure with .Stop method
func (ml *MockRouteCountLogger) Timer(title string) *Timer {
return ml.logger.Timer(title)
}
// TraceD implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) TraceD(title string, data map[string]interface{}) {
ml.logger.TraceD(title, data)
}
// DebugD implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) DebugD(title string, data map[string]interface{}) {
ml.logger.DebugD(title, data)
}
// InfoD implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) InfoD(title string, data map[string]interface{}) {
ml.logger.InfoD(title, data)
}
// WarnD implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) WarnD(title string, data map[string]interface{}) {
ml.logger.WarnD(title, data)
}
// ErrorD implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) ErrorD(title string, data map[string]interface{}) {
ml.logger.ErrorD(title, data)
}
// CriticalD implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) CriticalD(title string, data map[string]interface{}) {
ml.logger.CriticalD(title, data)
}
// CounterD implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) CounterD(title string, value int, data map[string]interface{}) {
ml.logger.CounterD(title, value, data)
}
// GaugeIntD implements the method for the KayveeLogger interface.
func (ml *MockRouteCountLogger) GaugeIntD(title string, value int, data map[string]interface{}) {
ml.logger.GaugeIntD(title, value, data)
}
// GaugeFloatD implements the method for the KayveeLogger interface.
// Logs with type = gauge, and value = value
func (ml *MockRouteCountLogger) GaugeFloatD(title string, value float64, data map[string]interface{}) {
ml.logger.GaugeFloatD(title, value, data)
}
// TimerD implements the method for the KayveeLogger interface.
// Returns Timer structure with .Stop method
func (ml *MockRouteCountLogger) TimerD(title string, data map[string]interface{}) *Timer {
return ml.logger.TimerD(title, data)
}