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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
// this is a new logger interface for mattermost
package logger
type LogMessage struct {
Context map[string]string
File string
Message string
}
const validPath = `^utils/([a-z_]+/)*logger_test.go$`
/*
Broken because code somehow depends on path
// ensures that the relative path of the file that called into the logger is returned
func TestGetCallerFilename(t *testing.T) {
filename, _ := getCallerFilename()
fmt.Println("Thing: " + filename)
matched, err := regexp.MatchString(validPath, filename)
require.NoError(t, err)
assert.True(t, matched)
}
// ensures that values can be recorded on a Context object, and that the data in question is serialized as a part of the log message
func TestSerializeContext(t *testing.T) {
ctx := context.Background()
expectedUserId := "some-fake-user-id"
ctx = WithUserId(ctx, expectedUserId)
expectedRequestId := "some-fake-request-id"
ctx = WithRequestId(ctx, expectedRequestId)
serialized := serializeContext(ctx)
assert.Equal(t, map[string]string{
"user_id": expectedUserId,
"request_id": expectedRequestId,
}, serialized)
}
// ensures that an entire log message with an empty context can be properly serialized into a JSON object
func TestSerializeLogMessageEmptyContext(t *testing.T) {
emptyContext := context.Background()
var logMessage = "This is a log message"
var serialized = serializeLogMessage(emptyContext, logMessage)
var deserialized LogMessage
json.Unmarshal([]byte(serialized), &deserialized)
assert.Empty(t, deserialized.Context)
assert.Equal(t, logMessage, deserialized.Message)
matched, err := regexp.MatchString(validPath, deserialized.File)
require.NoError(t, err)
assert.True(t, matched)
}
// ensures that an entire log message with a populated context can be properly serialized into a JSON object
func TestSerializeLogMessagePopulatedContext(t *testing.T) {
populatedContext := context.Background()
populatedContext = WithRequestId(populatedContext, "foo")
populatedContext = WithUserId(populatedContext, "bar")
var logMessage = "This is a log message"
var serialized = serializeLogMessage(populatedContext, logMessage)
var deserialized LogMessage
json.Unmarshal([]byte(serialized), &deserialized)
assert.Equal(t, map[string]string{
"request_id": "foo",
"user_id": "bar",
}, deserialized.Context)
assert.Equal(t, logMessage, deserialized.Message)
matched, err := regexp.MatchString(validPath, deserialized.File)
require.NoError(t, err)
assert.True(t, matched)
}
// ensures that a debugLog message is passed through to the underlying logger as expected
func TestDebugc(t *testing.T) {
// inject a "mocked" debugLog method that captures the first argument that is passed to it
var capture string
oldDebug := debugLog
defer func() { debugLog = oldDebug }()
type WrapperType func() string
debugLog = func(format interface{}, args ...interface{}) {
// the code that we're testing passes a closure to the debugLog method, so we have to execute it to get the actual message back
if f, ok := format.(func() string); ok {
capture = WrapperType(f)()
} else {
t.Error("First parameter passed to Debug is not a closure")
}
}
// log something
emptyContext := context.Background()
var logMessage = "Some log message"
Debugc(emptyContext, logMessage)
// check to see that the message is logged to the underlying log system, in this case our mock method
var deserialized LogMessage
json.Unmarshal([]byte(capture), &deserialized)
assert.Empty(t, deserialized.Context)
assert.Equal(t, logMessage, deserialized.Message)
matched, err := regexp.MatchString(validPath, deserialized.File)
require.NoError(t, err)
assert.True(t, matched)
}
// ensures that a debugLog message is passed through to the underlying logger as expected
func TestDebugf(t *testing.T) {
// inject a "mocked" debugLog method that captures the first argument that is passed to it
var capture string
oldDebug := debugLog
defer func() { debugLog = oldDebug }()
type WrapperType func() string
debugLog = func(format interface{}, args ...interface{}) {
// the code that we're testing passes a closure to the debugLog method, so we have to execute it to get the actual message back
if f, ok := format.(func() string); ok {
capture = WrapperType(f)()
} else {
t.Error("First parameter passed to Debug is not a closure")
}
}
// log something
formatString := "Some %v message"
param := "log"
Debugf(formatString, param)
// check to see that the message is logged to the underlying log system, in this case our mock method
var deserialized LogMessage
json.Unmarshal([]byte(capture), &deserialized)
assert.Empty(t, deserialized.Context)
assert.Equal(t, fmt.Sprintf(formatString, param), deserialized.Message)
matched, err := regexp.MatchString(validPath, deserialized.File)
require.NoError(t, err)
assert.True(t, matched)
}
// ensures that an infoLog message is passed through to the underlying logger as expected
func TestInfoc(t *testing.T) {
// inject a "mocked" infoLog method that captures the first argument that is passed to it
var capture string
oldInfo := infoLog
defer func() { infoLog = oldInfo }()
type WrapperType func() string
infoLog = func(format interface{}, args ...interface{}) {
// the code that we're testing passes a closure to the infoLog method, so we have to execute it to get the actual message back
if f, ok := format.(func() string); ok {
capture = WrapperType(f)()
} else {
t.Error("First parameter passed to Info is not a closure")
}
}
// log something
emptyContext := context.Background()
var logMessage = "Some log message"
Infoc(emptyContext, logMessage)
// check to see that the message is logged to the underlying log system, in this case our mock method
var deserialized LogMessage
json.Unmarshal([]byte(capture), &deserialized)
assert.Empty(t, deserialized.Context)
assert.Equal(t, logMessage, deserialized.Message)
matched, err := regexp.MatchString(validPath, deserialized.File)
require.NoError(t, err)
assert.True(t, matched)
}
// ensures that an infoLog message is passed through to the underlying logger as expected
func TestInfof(t *testing.T) {
// inject a "mocked" infoLog method that captures the first argument that is passed to it
var capture string
oldInfo := infoLog
defer func() { infoLog = oldInfo }()
type WrapperType func() string
infoLog = func(format interface{}, args ...interface{}) {
// the code that we're testing passes a closure to the infoLog method, so we have to execute it to get the actual message back
if f, ok := format.(func() string); ok {
capture = WrapperType(f)()
} else {
t.Error("First parameter passed to Info is not a closure")
}
}
// log something
format := "Some %v message"
param := "log"
Infof(format, param)
// check to see that the message is logged to the underlying log system, in this case our mock method
var deserialized LogMessage
json.Unmarshal([]byte(capture), &deserialized)
assert.Empty(t, deserialized.Context)
assert.Equal(t, fmt.Sprintf(format, param), deserialized.Message)
matched, err := regexp.MatchString(validPath, deserialized.File)
require.NoError(t, err)
assert.True(t, matched)
}
// ensures that an error message is passed through to the underlying logger as expected
func TestErrorc(t *testing.T) {
// inject a "mocked" err method that captures the first argument that is passed to it
var capture string
oldError := errorLog
defer func() { errorLog = oldError }()
type WrapperType func() string
errorLog = func(format interface{}, args ...interface{}) error {
// the code that we're testing passes a closure to the err method, so we have to execute it to get the actual message back
if f, ok := format.(func() string); ok {
capture = WrapperType(f)()
} else {
t.Error("First parameter passed to Error is not a closure")
}
// the code under test doesn't care about this return value
return errors.New(capture)
}
// log something
emptyContext := context.Background()
var logMessage = "Some log message"
Errorc(emptyContext, logMessage)
// check to see that the message is logged to the underlying log system, in this case our mock method
var deserialized LogMessage
json.Unmarshal([]byte(capture), &deserialized)
assert.Empty(t, deserialized.Context)
assert.Equal(t, logMessage, deserialized.Message)
matched, err := regexp.MatchString(validPath, deserialized.File)
require.NoError(t, err)
assert.True(t, matched)
}
// ensures that an error message is passed through to the underlying logger as expected
func TestErrorf(t *testing.T) {
// inject a "mocked" err method that captures the first argument that is passed to it
var capture string
oldError := errorLog
defer func() { errorLog = oldError }()
type WrapperType func() string
errorLog = func(format interface{}, args ...interface{}) error {
// the code that we're testing passes a closure to the err method, so we have to execute it to get the actual message back
if f, ok := format.(func() string); ok {
capture = WrapperType(f)()
} else {
t.Error("First parameter passed to Error is not a closure")
}
// the code under test doesn't care about this return value
return errors.New(capture)
}
// log something
format := "Some %v message"
param := "log"
Errorf(format, param)
// check to see that the message is logged to the underlying log system, in this case our mock method
var deserialized LogMessage
json.Unmarshal([]byte(capture), &deserialized)
assert.Empty(t, deserialized.Context)
assert.Equal(t, fmt.Sprintf(format, param), deserialized.Message)
matched, err := regexp.MatchString(validPath, deserialized.File)
require.NoError(t, err)
assert.True(t, matched)
}
*/
|