-
Notifications
You must be signed in to change notification settings - Fork 1
/
wlog_test.go
153 lines (118 loc) · 2.48 KB
/
wlog_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
package wlog
import (
"bytes"
"encoding/json"
"testing"
"time"
)
func TestHooks(t *testing.T) {
// Install a hook to catch all NFO (Info) messages
InstallHook(Nfo, func(timestamp time.Time, logLevel LogLevel, message string) {
if logLevel != Nfo {
t.Fatalf("Expected %s but got %s", Nfo, logLevel)
}
})
// Install a hook to catch all WRN (Warning) messages
InstallHook(Wrn, func(timestamp time.Time, logLevel LogLevel, message string) {
if logLevel != Wrn {
t.Fatalf("Expected %s but got %s", Wrn, logLevel)
}
})
Info("This is a NFO log entry")
Warning("This is a WRN log entry")
Error("This is a ERR log entry. No hooks installed for this level")
}
func TestFieldMapping(t *testing.T) {
SetFormatter(JSONFormatter{
Compact: true,
})
SetStdOut(false)
w := &bytes.Buffer{}
SetWriter(w)
SetFieldMapping(FieldMapping{"name": "n", "address": "addr", "tenantId": "tid"})
logger := WithScope(Fields{"tenantId": "1223456", "name": "user", "address": "my street"})
logger.Info("This is a test")
reader := bytes.NewReader(w.Bytes())
d := json.NewDecoder(reader)
var data map[string]string
d.Decode(&data)
expectedKeys := []string{"@t", "@m", "@l", "tid", "n", "addr"}
for _, k := range expectedKeys {
if _, ok := data[k]; !ok {
t.Fatalf("should contain a key: %s", k)
}
}
}
func TestLogLevel(t *testing.T) {
txt := "data"
SetStdOut(false)
w := &bytes.Buffer{}
SetWriter(w)
SetLogLevel(Dbg)
Debug(txt)
if w.Len() == 0 {
t.Fatalf("expected log output")
}
w.Reset()
Info(txt)
if w.Len() == 0 {
t.Fatalf("expected log output")
}
SetLogLevel(Nfo)
w.Reset()
Debug(txt)
if w.Len() > 0 {
t.Fatalf("unexpected log output")
}
w.Reset()
Info(txt)
if w.Len() == 0 {
t.Fatalf("expected log output")
}
w.Reset()
Warningf(txt)
if w.Len() == 0 {
t.Fatalf("expected log output")
}
SetLogLevel(Ftl)
w.Reset()
Debug(txt)
if w.Len() > 0 {
t.Fatalf("unexpected log output")
}
w.Reset()
Debugf(txt)
if w.Len() > 0 {
t.Fatalf("unexpected log output")
}
w.Reset()
Info(txt)
if w.Len() > 0 {
t.Fatalf("unexpected log output")
}
w.Reset()
Infof(txt)
if w.Len() > 0 {
t.Fatalf("unexpected log output")
}
w.Reset()
Warning(txt)
if w.Len() > 0 {
t.Fatalf("unexpected log output")
}
w.Reset()
Warningf(txt)
if w.Len() > 0 {
t.Fatalf("unexpected log output")
}
w.Reset()
Error(txt)
if w.Len() > 0 {
t.Fatalf("unexpected log output")
}
w.Reset()
Errorf(txt)
if w.Len() > 0 {
t.Fatalf("unexpected log output")
}
}