forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger_test.go
284 lines (234 loc) · 5.73 KB
/
logger_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
package log
import (
"bytes"
"errors"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/thrasher-corp/gocryptotrader/common/convert"
)
func TestMain(m *testing.M) {
setupTestLoggers()
os.Exit(m.Run())
}
func setupTestLoggers() {
logTest := Config{
Enabled: convert.BoolPtr(true),
SubLoggerConfig: SubLoggerConfig{
Output: "console",
Level: "INFO|WARN|DEBUG|ERROR",
},
AdvancedSettings: advancedSettings{
ShowLogSystemName: convert.BoolPtr(true),
Spacer: " | ",
TimeStampFormat: timestampFormat,
Headers: headers{
Info: "[INFO]",
Warn: "[WARN]",
Debug: "[DEBUG]",
Error: "[ERROR]",
},
},
SubLoggers: []SubLoggerConfig{
{
Name: "TEST",
Level: "INFO|DEBUG|WARN|ERROR",
Output: "stdout",
}},
}
RWM.Lock()
GlobalLogConfig = &logTest
RWM.Unlock()
SetupGlobalLogger()
SetupSubLoggers(logTest.SubLoggers)
}
func SetupDisabled() {
logTest := Config{
Enabled: convert.BoolPtr(false),
}
RWM.Lock()
GlobalLogConfig = &logTest
RWM.Unlock()
SetupGlobalLogger()
SetupSubLoggers(logTest.SubLoggers)
}
func BenchmarkInfo(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
Info(Global, "Hello this is an info benchmark")
}
}
func SetupTestDisabled(t *testing.T) {
SetupDisabled()
}
func TestAddWriter(t *testing.T) {
mw := MultiWriter()
m := mw.(*multiWriter)
m.Add(ioutil.Discard)
m.Add(os.Stdin)
m.Add(os.Stdout)
total := len(m.writers)
if total != 3 {
t.Errorf("expected m.Writers to be 3 %v", total)
}
}
func TestRemoveWriter(t *testing.T) {
mw := MultiWriter()
m := mw.(*multiWriter)
m.Add(ioutil.Discard)
m.Add(os.Stdin)
m.Add(os.Stdout)
total := len(m.writers)
m.Remove(os.Stdin)
m.Remove(os.Stdout)
if len(m.writers) != total-2 {
t.Errorf("expected m.Writers to be %v got %v", total-2, len(m.writers))
}
}
func TestLevel(t *testing.T) {
_, err := Level("LOG")
if err != nil {
t.Errorf("Failed to get log %s levels skipping", err)
}
_, err = Level("totallyinvalidlogger")
if err == nil {
t.Error("Expected error on invalid logger")
}
}
func TestSetLevel(t *testing.T) {
newLevel, err := SetLevel("LOG", "ERROR")
if err != nil {
t.Skipf("Failed to get log %s levels skipping", err)
}
if newLevel.Info || newLevel.Debug || newLevel.Warn {
t.Error("failed to set level correctly")
}
if !newLevel.Error {
t.Error("failed to set level correctly")
}
_, err = SetLevel("abc12345556665", "ERROR")
if err == nil {
t.Error("SetLevel() Should return error on invalid logger")
}
}
func TestValidSubLogger(t *testing.T) {
b, logPtr := validSubLogger("LOG")
if !b {
t.Skip("validSubLogger() should return found, pointer if valid logger found")
}
if logPtr == nil {
t.Error("validSubLogger() should return a pointer and not nil")
}
}
func TestCloseLogger(t *testing.T) {
err := CloseLogger()
if err != nil {
t.Errorf("CloseLogger() failed %v", err)
}
}
func TestConfigureSubLogger(t *testing.T) {
err := configureSubLogger("LOG", "INFO", os.Stdin)
if err != nil {
t.Skipf("configureSubLogger() returned unexpected error %v", err)
}
if (Global.Levels != Levels{
Info: true,
Debug: false,
}) {
t.Error("configureSubLogger() incorrectly configure subLogger")
}
if Global.name != "LOG" {
t.Error("configureSubLogger() Failed to uppercase name")
}
}
func TestSplitLevel(t *testing.T) {
levelsInfoDebug := splitLevel("INFO|DEBUG")
expected := Levels{
Info: true,
Debug: true,
Warn: false,
Error: false,
}
if levelsInfoDebug != expected {
t.Errorf("splitLevel() returned invalid data expected: %+v got: %+v", expected, levelsInfoDebug)
}
}
func BenchmarkInfoDisabled(b *testing.B) {
SetupDisabled()
b.ResetTimer()
for n := 0; n < b.N; n++ {
Info(Global, "Hello this is an info benchmark")
}
}
func BenchmarkInfof(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
Infof(Global, "Hello this is an infof benchmark %v %v %v\n", n, 1, 2)
}
}
func BenchmarkInfoln(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
Infoln(Global, "Hello this is an infoln benchmark")
}
}
func TestNewLogEvent(t *testing.T) {
w := &bytes.Buffer{}
logger.newLogEvent("out", "header", "SUBLOGGER", w)
if w.String() == "" {
t.Error("newLogEvent() failed expected output got empty string")
}
err := logger.newLogEvent("out", "header", "SUBLOGGER", nil)
if err == nil {
t.Error("Error expected with output is set to nil")
}
}
func TestInfo(t *testing.T) {
w := &bytes.Buffer{}
tempSL := SubLogger{
"TESTYMCTESTALOT",
splitLevel("INFO|WARN|DEBUG|ERROR"),
w,
}
Info(&tempSL, "Hello")
if w.String() == "" {
t.Error("expected Info() to write output to buffer")
}
tempSL.output = nil
w.Reset()
SetLevel("TESTYMCTESTALOT", "INFO")
Debug(&tempSL, "HelloHello")
if w.String() != "" {
t.Error("Expected output buffer to be empty but Debug wrote to output")
}
}
func TestSubLoggerName(t *testing.T) {
w := &bytes.Buffer{}
registerNewSubLogger("sublogger")
logger.newLogEvent("out", "header", "SUBLOGGER", w)
if !strings.Contains(w.String(), "SUBLOGGER") {
t.Error("Expected SUBLOGGER in output")
}
logger.ShowLogSystemName = false
w.Reset()
logger.newLogEvent("out", "header", "SUBLOGGER", w)
if strings.Contains(w.String(), "SUBLOGGER") {
t.Error("Unexpected SUBLOGGER in output")
}
}
func TestNewSubLogger(t *testing.T) {
_, err := NewSubLogger("")
if !errors.Is(err, errEmptyLoggerName) {
t.Fatalf("received: %v but expected: %v", err, errEmptyLoggerName)
}
sl, err := NewSubLogger("TESTERINOS")
if !errors.Is(err, nil) {
t.Fatalf("received: %v but expected: %v", err, nil)
}
Debug(sl, "testerinos")
_, err = NewSubLogger("TESTERINOS")
if !errors.Is(err, errSubLoggerAlreadyregistered) {
t.Fatalf("received: %v but expected: %v", err, errSubLoggerAlreadyregistered)
}
}