forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_test.go
342 lines (281 loc) · 9.03 KB
/
log_test.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright (c) 2023 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"bytes"
"context"
"errors"
"fmt"
"strings"
"testing"
"time"
rlog "github.com/sirupsen/logrus"
)
func createTestLogger() defaultLogger {
var rLogger = rlog.New()
var ret = defaultLogger{inner: rLogger}
return ret
}
func TestIsLevelEnabled(t *testing.T) {
logger := createTestLogger()
logger.SetLevel(rlog.TraceLevel)
if !logger.IsLevelEnabled(rlog.TraceLevel) {
t.Fatalf("log level should be trace but is %v", logger.GetLevel())
}
}
func TestLogFunction(t *testing.T) {
logger := createTestLogger()
buf := &bytes.Buffer{}
var formatter = rlog.TextFormatter{CallerPrettyfier: SFCallerPrettyfier}
logger.SetFormatter(&formatter)
logger.SetReportCaller(true)
logger.SetOutput(buf)
logger.SetLevel(rlog.TraceLevel)
logger.Log(rlog.TraceLevel, "hello world")
logger.Logf(rlog.TraceLevel, "log %v", "format")
logger.Logln(rlog.TraceLevel, "log line")
var strbuf = buf.String()
if !strings.Contains(strbuf, "hello world") &&
!strings.Contains(strbuf, "log format") &&
!strings.Contains(strbuf, "log line") {
t.Fatalf("unexpected output in log %v", strbuf)
}
}
func TestSetLogLevelError(t *testing.T) {
logger := CreateDefaultLogger()
err := logger.SetLogLevel("unknown")
if err == nil {
t.Fatal("should have thrown an error")
}
}
func TestDefaultLogLevel(t *testing.T) {
logger := CreateDefaultLogger()
buf := &bytes.Buffer{}
logger.SetOutput(buf)
SetLogger(&logger)
// default logger level is info
logger.Info("info")
logger.Infof("info%v", "f")
logger.Infoln("infoln")
// debug and trace won't write to log since they are higher than info level
logger.Debug("debug")
logger.Debugf("debug%v", "f")
logger.Debugln("debugln")
logger.Trace("trace")
logger.Tracef("trace%v", "f")
logger.Traceln("traceln")
// print, warning and error should write to log since they are lower than info
logger.Print("print")
logger.Printf("print%v", "f")
logger.Println("println")
logger.Warn("warn")
logger.Warnf("warn%v", "f")
logger.Warnln("warnln")
logger.Warning("warning")
logger.Warningf("warning%v", "f")
logger.Warningln("warningln")
logger.Error("error")
logger.Errorf("error%v", "f")
logger.Errorln("errorln")
// verify output
var strbuf = buf.String()
if strings.Contains(strbuf, "debug") &&
strings.Contains(strbuf, "trace") &&
!strings.Contains(strbuf, "info") &&
!strings.Contains(strbuf, "print") &&
!strings.Contains(strbuf, "warn") &&
!strings.Contains(strbuf, "warning") &&
!strings.Contains(strbuf, "error") {
t.Fatalf("unexpected output in log: %v", strbuf)
}
}
func TestOffLogLevel(t *testing.T) {
logger := CreateDefaultLogger()
buf := &bytes.Buffer{}
logger.SetOutput(buf)
err := logger.SetLogLevel("OFF")
assertNilF(t, err)
SetLogger(&logger)
logger.Info("info")
logger.Infof("info%v", "f")
logger.Infoln("infoln")
logger.Debug("debug")
logger.Debugf("debug%v", "f")
logger.Debugln("debugln")
logger.Trace("trace")
logger.Tracef("trace%v", "f")
logger.Traceln("traceln")
logger.Print("print")
logger.Printf("print%v", "f")
logger.Println("println")
logger.Warn("warn")
logger.Warnf("warn%v", "f")
logger.Warnln("warnln")
logger.Warning("warning")
logger.Warningf("warning%v", "f")
logger.Warningln("warningln")
logger.Error("error")
logger.Errorf("error%v", "f")
logger.Errorln("errorln")
assertEqualE(t, buf.Len(), 0, "log messages count")
assertEqualE(t, logger.GetLogLevel(), "OFF", "log level")
}
func TestLogSetLevel(t *testing.T) {
logger := GetLogger()
buf := &bytes.Buffer{}
logger.SetOutput(buf)
logger.SetLogLevel("trace")
logger.Trace("should print at trace level")
logger.Debug("should print at debug level")
var strbuf = buf.String()
if !strings.Contains(strbuf, "trace level") &&
!strings.Contains(strbuf, "debug level") {
t.Fatalf("unexpected output in log: %v", strbuf)
}
}
func TestLogWithError(t *testing.T) {
logger := CreateDefaultLogger()
buf := &bytes.Buffer{}
logger.SetOutput(buf)
err := errors.New("error")
logger.WithError(err).Info("hello world")
var strbuf = buf.String()
if !strings.Contains(strbuf, "error=error") {
t.Fatalf("unexpected output in log: %v", strbuf)
}
}
func TestLogWithTime(t *testing.T) {
logger := createTestLogger()
buf := &bytes.Buffer{}
logger.SetOutput(buf)
ti := time.Now()
logger.WithTime(ti).Info("hello")
time.Sleep(3 * time.Second)
var strbuf = buf.String()
if !strings.Contains(strbuf, ti.Format(time.RFC3339)) {
t.Fatalf("unexpected string in output: %v", strbuf)
}
}
func TestLogWithField(t *testing.T) {
logger := CreateDefaultLogger()
buf := &bytes.Buffer{}
logger.SetOutput(buf)
logger.WithField("field", "test").Info("hello")
var strbuf = buf.String()
if !strings.Contains(strbuf, "field=test") {
t.Fatalf("unexpected string in output: %v", strbuf)
}
}
func TestLogLevelFunctions(t *testing.T) {
logger := createTestLogger()
buf := &bytes.Buffer{}
logger.SetOutput(buf)
logger.TraceFn(func() []interface{} {
return []interface{}{
"trace function",
}
})
logger.DebugFn(func() []interface{} {
return []interface{}{
"debug function",
}
})
logger.InfoFn(func() []interface{} {
return []interface{}{
"info function",
}
})
logger.PrintFn(func() []interface{} {
return []interface{}{
"print function",
}
})
logger.WarningFn(func() []interface{} {
return []interface{}{
"warning function",
}
})
logger.ErrorFn(func() []interface{} {
return []interface{}{
"error function",
}
})
// check that info, print, warning and error were outputted to the log.
var strbuf = buf.String()
if strings.Contains(strbuf, "debug") &&
strings.Contains(strbuf, "trace") &&
!strings.Contains(strbuf, "info") &&
!strings.Contains(strbuf, "print") &&
!strings.Contains(strbuf, "warning") &&
!strings.Contains(strbuf, "error") {
t.Fatalf("unexpected output in log: %v", strbuf)
}
}
type testRequestIDCtxKey struct{}
func TestLogKeysDefault(t *testing.T) {
logger := CreateDefaultLogger()
buf := &bytes.Buffer{}
logger.SetOutput(buf)
ctx := context.Background()
// set the sessionID on the context to see if we have it in the logs
sessionIDContextValue := "sessionID"
ctx = context.WithValue(ctx, SFSessionIDKey, sessionIDContextValue)
userContextValue := "madison"
ctx = context.WithValue(ctx, SFSessionUserKey, userContextValue)
// base case (not using RegisterContextVariableToLog to add additional types )
logger.WithContext(ctx).Info("test")
var strbuf = buf.String()
if !strings.Contains(strbuf, fmt.Sprintf("%s=%s", SFSessionIDKey, sessionIDContextValue)) {
t.Fatalf("expected that sfSessionIdKey would be in logs if logger.WithContext was used, but got: %v", strbuf)
}
if !strings.Contains(strbuf, fmt.Sprintf("%s=%s", SFSessionUserKey, userContextValue)) {
t.Fatalf("expected that SFSessionUserKey would be in logs if logger.WithContext was used, but got: %v", strbuf)
}
}
func TestLogKeysWithRegisterContextVariableToLog(t *testing.T) {
logger := CreateDefaultLogger()
buf := &bytes.Buffer{}
logger.SetOutput(buf)
ctx := context.Background()
// set the sessionID on the context to see if we have it in the logs
sessionIDContextValue := "sessionID"
ctx = context.WithValue(ctx, SFSessionIDKey, sessionIDContextValue)
userContextValue := "testUser"
ctx = context.WithValue(ctx, SFSessionUserKey, userContextValue)
// test that RegisterContextVariableToLog works with non string keys
logKey := "REQUEST_ID"
contextIntVal := 123
ctx = context.WithValue(ctx, testRequestIDCtxKey{}, contextIntVal)
getRequestKeyFunc := func(ctx context.Context) string {
if requestContext, ok := ctx.Value(testRequestIDCtxKey{}).(int); ok {
return fmt.Sprint(requestContext)
}
return ""
}
RegisterLogContextHook(logKey, getRequestKeyFunc)
// base case (not using RegisterContextVariableToLog to add additional types )
logger.WithContext(ctx).Info("test")
var strbuf = buf.String()
if !strings.Contains(strbuf, fmt.Sprintf("%s=%s", SFSessionIDKey, sessionIDContextValue)) {
t.Fatalf("expected that sfSessionIdKey would be in logs if logger.WithContext and RegisterContextVariableToLog was used, but got: %v", strbuf)
}
if !strings.Contains(strbuf, fmt.Sprintf("%s=%s", SFSessionUserKey, userContextValue)) {
t.Fatalf("expected that SFSessionUserKey would be in logs if logger.WithContext and RegisterContextVariableToLog was used, but got: %v", strbuf)
}
if !strings.Contains(strbuf, fmt.Sprintf("%s=%s", logKey, fmt.Sprint(contextIntVal))) {
t.Fatalf("expected that REQUEST_ID would be in logs if logger.WithContext and RegisterContextVariableToLog was used, but got: %v", strbuf)
}
}
func TestLogMaskSecrets(t *testing.T) {
logger := CreateDefaultLogger()
buf := &bytes.Buffer{}
logger.SetOutput(buf)
ctx := context.Background()
query := "create user testuser password='testpassword'"
logger.WithContext(ctx).Infof("Query: %#v", query)
// verify output
expected := "create user testuser password='****"
var strbuf = buf.String()
if !strings.Contains(strbuf, expected) {
t.Fatalf("expected that password would be masked. WithContext was used, but got: %v", strbuf)
}
}