-
Notifications
You must be signed in to change notification settings - Fork 0
/
std_level_logger_test.go
214 lines (166 loc) · 7.02 KB
/
std_level_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
package adaptlog
import (
"strings"
"testing"
)
func TestLogLevelToString(t *testing.T) {
if DebugLevel.String() != "Debug" {
t.Fatal("Should have been Debug")
}
if InfoLevel.String() != "Info" {
t.Fatal("Should have been Info")
}
if WarnLevel.String() != "Warn" {
t.Fatal("Should have been Warn")
}
if ErrorLevel.String() != "Error" {
t.Fatal("Should have been Error")
}
if FatalLevel.String() != "Fatal" {
t.Fatal("Should have been Fatal")
}
if LogLevel(9).String() != "Not mapped value 9" {
t.Fatal("Should have been 'Not mapped value 9'")
}
}
func TestStdLevelLoggerNew(t *testing.T) {
ConfigureStdLevelLogger(DebugLevel, nil, "")
if Level == nil {
t.Fatal("Should have returned a error")
}
}
func TestStdLevelLoggerNewWithWriter(t *testing.T) {
ConfigureStdLevelLogger(DebugLevel, new(TestWriter), "")
if Level == nil {
t.Fatal("Should have returned a error")
}
}
func TestStdLevelLoggerWithHigherDefaultLevel(t *testing.T) {
w := new(TestWriter)
ConfigureStdLevelLogger(ErrorLevel, w, "")
Level.Warn("Test")
Level.Error("Test")
Level.Fatal("Test")
if len(w.data) != 2 {
t.Fatalf("Data should have been 2 %s", w.data)
}
if !strings.HasSuffix(w.data[0], "Error Test\n") {
t.Fatalf("Expected %s actual %s", "Error Test", w.data[0])
}
if !strings.HasSuffix(w.data[1], "Fatal Test\n") {
t.Fatalf("Expected %s actual %s", "Error Test", w.data[0])
}
w.data = nil
Level.Warnf("Test")
Level.Errorf("Test")
Level.Fatalf("Test")
if len(w.data) != 2 {
t.Fatalf("Data should have been 2 %s", w.data)
}
if !strings.HasSuffix(w.data[0], "Error Test\n") {
t.Fatalf("Expected %s actual %s", "Error Test", w.data[0])
}
if !strings.HasSuffix(w.data[1], "Fatal Test\n") {
t.Fatalf("Expected %s actual %s", "Error Test", w.data[0])
}
w.data = nil
Level.Warnln("Test")
Level.Errorln("Test")
Level.Fatalln("Test")
if len(w.data) != 2 {
t.Fatalf("Data should have been 2 %s", w.data)
}
if !strings.HasSuffix(w.data[0], "Error Test\n") {
t.Fatalf("Expected %s actual %s", "Error Test", w.data[0])
}
if !strings.HasSuffix(w.data[1], "Fatal Test\n") {
t.Fatalf("Expected %s actual %s", "Error Test", w.data[0])
}
}
func TestStdLevelLoggerWithContext(t *testing.T) {
w := new(TestWriter)
ConfigureStdLevelLogger(DebugLevel, w, "Context")
Level.Debug("Test")
Level.Debugf("Test %s", "one")
Level.Debugln("Test")
if len(w.data) != 3 {
t.Fatalf("Data should have been 3 %s", w.data)
}
if !strings.HasSuffix(w.data[0], "Debug Context Test\n") {
t.Fatalf("Expected %s actual %s", "Debug Context Test", w.data[0])
}
if !strings.HasSuffix(w.data[1], "Debug Context Test one\n") {
t.Fatalf("Expected %s actual %s", "Debug Context Test one", w.data[1])
}
if !strings.HasSuffix(w.data[2], "Debug Context Test\n") {
t.Fatalf("Expected %s actual %s", "Debug Context Test", w.data[2])
}
}
var stdLevelLogTests = []struct {
in contextLevelLogTestCase
out string
}{
{contextLevelLogTestCase{"Debug", "", []string{"arg1", "arg2", "arg3"}}, "Debug arg1arg2arg3\n"},
{contextLevelLogTestCase{"Debugf", "args=%s,%s,%s", []string{"arg1", "arg2", "arg3"}}, "Debug args=arg1,arg2,arg3\n"},
{contextLevelLogTestCase{"Debugln", "", []string{"arg1", "arg2", "arg3"}}, "Debug arg1 arg2 arg3\n"},
{contextLevelLogTestCase{"Info", "", []string{"arg1", "arg2", "arg3"}}, "Info arg1arg2arg3\n"},
{contextLevelLogTestCase{"Infof", "args=%s,%s,%s", []string{"arg1", "arg2", "arg3"}}, "Info args=arg1,arg2,arg3\n"},
{contextLevelLogTestCase{"Infoln", "", []string{"arg1", "arg2", "arg3"}}, "Info arg1 arg2 arg3\n"},
{contextLevelLogTestCase{"Warn", "", []string{"arg1", "arg2", "arg3"}}, "Warn arg1arg2arg3\n"},
{contextLevelLogTestCase{"Warnf", "args=%s,%s,%s", []string{"arg1", "arg2", "arg3"}}, "Warn args=arg1,arg2,arg3\n"},
{contextLevelLogTestCase{"Warnln", "", []string{"arg1", "arg2", "arg3"}}, "Warn arg1 arg2 arg3\n"},
{contextLevelLogTestCase{"Error", "", []string{"arg1", "arg2", "arg3"}}, "Error arg1arg2arg3\n"},
{contextLevelLogTestCase{"Errorf", "args=%s,%s,%s", []string{"arg1", "arg2", "arg3"}}, "Error args=arg1,arg2,arg3\n"},
{contextLevelLogTestCase{"Errorln", "", []string{"arg1", "arg2", "arg3"}}, "Error arg1 arg2 arg3\n"},
{contextLevelLogTestCase{"Fatal", "", []string{"arg1", "arg2", "arg3"}}, "Fatal arg1arg2arg3\n"},
{contextLevelLogTestCase{"Fatalf", "args=%s,%s,%s", []string{"arg1", "arg2", "arg3"}}, "Fatal args=arg1,arg2,arg3\n"},
{contextLevelLogTestCase{"Fatalln", "", []string{"arg1", "arg2", "arg3"}}, "Fatal arg1 arg2 arg3\n"},
}
func TestStdLeveledLogger(t *testing.T) {
w := new(TestWriter)
ConfigureStdLevelLogger(DebugLevel, w, "")
logFunctions := make(map[string]logFunction, 0)
logFunctions["Debug"] = func(msg string, args ...string) { Level.Debug(convertToInterfaceSlice(args)...) }
logFunctions["Debugf"] = func(msg string, args ...string) { Level.Debugf(msg, convertToInterfaceSlice(args)...) }
logFunctions["Debugln"] = func(msg string, args ...string) { Level.Debugln(convertToInterfaceSlice(args)...) }
logFunctions["Info"] = func(msg string, args ...string) { Level.Info(convertToInterfaceSlice(args)...) }
logFunctions["Infof"] = func(msg string, args ...string) { Level.Infof(msg, convertToInterfaceSlice(args)...) }
logFunctions["Infoln"] = func(msg string, args ...string) { Level.Infoln(convertToInterfaceSlice(args)...) }
logFunctions["Warn"] = func(msg string, args ...string) { Level.Warn(convertToInterfaceSlice(args)...) }
logFunctions["Warnf"] = func(msg string, args ...string) { Level.Warnf(msg, convertToInterfaceSlice(args)...) }
logFunctions["Warnln"] = func(msg string, args ...string) { Level.Warnln(convertToInterfaceSlice(args)...) }
logFunctions["Error"] = func(msg string, args ...string) { Level.Error(convertToInterfaceSlice(args)...) }
logFunctions["Errorf"] = func(msg string, args ...string) { Level.Errorf(msg, convertToInterfaceSlice(args)...) }
logFunctions["Errorln"] = func(msg string, args ...string) { Level.Errorln(convertToInterfaceSlice(args)...) }
logFunctions["Fatal"] = func(msg string, args ...string) { Level.Fatal(convertToInterfaceSlice(args)...) }
logFunctions["Fatalf"] = func(msg string, args ...string) { Level.Fatalf(msg, convertToInterfaceSlice(args)...) }
logFunctions["Fatalln"] = func(msg string, args ...string) { Level.Fatalln(convertToInterfaceSlice(args)...) }
for _, testCase := range stdLevelLogTests {
w.data = nil
logFunctions[testCase.in.logFunctionName](testCase.in.msg, testCase.in.args...)
if !strings.HasSuffix(w.data[0], testCase.out) {
t.Fatalf("Case %s: Expected [%s] Actual [%s]", testCase.in.logFunctionName, testCase.out, w.data[0])
}
}
}
type TestWriter struct {
data []string
}
func (t *TestWriter) Write(p []byte) (n int, err error) {
var text = string(p)
t.data = append(t.data, text)
return len(text), nil
}
type logFunction func(string, ...string)
func convertToInterfaceSlice(a []string) []interface{} {
b := make([]interface{}, len(a))
for i := range a {
b[i] = a[i]
}
return b
}
type contextLevelLogTestCase struct {
logFunctionName string
msg string
args []string
}