-
Notifications
You must be signed in to change notification settings - Fork 7
/
std_test.go
165 lines (139 loc) · 3.96 KB
/
std_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
package log
import (
"bytes"
stdlog "log"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestStdLogger(t *testing.T) {
buf := &bytes.Buffer{}
stdlog.SetOutput(buf)
defer func() {
stdlog.SetOutput(os.Stderr)
}()
lgr := NewStandard()
// Make sure levels are working
lgr.Debug("test debug")
if strings.Contains(buf.String(), `[DEBUG] test debug`) {
t.Log(buf.String())
t.Error("stdlib debug not logging correctly")
}
buf.Reset()
// Test all levels
lgr.Level = TraceLevel
lgr.Trace("test debug")
if !strings.Contains(buf.String(), `[TRACE] test debug`) {
t.Log(buf.String())
t.Error("stdlib trace not logging correctly")
}
buf.Reset()
lgr.Tracef("Hello %s", "World")
if !strings.Contains(buf.String(), `[TRACE] Hello World`) {
t.Error("stdlib trace not logging correctly")
}
buf.Reset()
lgr.Tracew("foo bar", Fields{"baz": "qux"})
if !strings.Contains(buf.String(), `[TRACE] foo bar [baz=qux]`) {
t.Log(buf.String())
t.Error("stdlib trace not logging correctly")
}
buf.Reset()
lgr.Debug("test debug")
if !strings.Contains(buf.String(), `[DEBUG] test debug`) {
t.Log(buf.String())
t.Error("stdlib debug not logging correctly")
}
buf.Reset()
lgr.Debugf("Hello %s", "World")
if !strings.Contains(buf.String(), `[DEBUG] Hello World`) {
t.Error("stdlib debug not logging correctly")
}
buf.Reset()
lgr.Debugw("foo bar", Fields{"baz": "qux"})
if !strings.Contains(buf.String(), `[DEBUG] foo bar [baz=qux]`) {
t.Log(buf.String())
t.Error("stdlib debug not logging correctly")
}
buf.Reset()
lgr.Info("test info")
if !strings.Contains(buf.String(), `[INFO] test info`) {
t.Error("stdlib info not logging correctly")
}
buf.Reset()
lgr.Infof("Hello %s", "World")
if !strings.Contains(buf.String(), `[INFO] Hello World`) {
t.Error("stdlib info not logging correctly")
}
buf.Reset()
lgr.Infow("foo bar", Fields{"baz": "qux"})
if !strings.Contains(buf.String(), `[INFO] foo bar [baz=qux]`) {
t.Log(buf.String())
t.Error("stdlib info not logging correctly")
}
buf.Reset()
lgr.Warn("test warn")
if !strings.Contains(buf.String(), `[WARNING] test warn`) {
t.Log(buf.String())
t.Error("stdlib warn not logging correctly")
}
buf.Reset()
lgr.Warnf("Hello %s", "World")
if !strings.Contains(buf.String(), `[WARNING] Hello World`) {
t.Log(buf.String())
t.Error("stdlib warn not logging correctly")
}
buf.Reset()
lgr.Warnw("foo bar", Fields{"baz": "qux"})
if !strings.Contains(buf.String(), `[WARNING] foo bar [baz=qux]`) {
t.Log(buf.String())
t.Error("stdlib warn not logging correctly")
}
buf.Reset()
lgr.Error("test error")
if !strings.Contains(buf.String(), `[ERROR] test error`) {
t.Log(buf.String())
t.Error("stdlib error not logging correctly")
}
buf.Reset()
lgr.Errorf("Hello %s", "World")
if !strings.Contains(buf.String(), `[ERROR] Hello World`) {
t.Log(buf.String())
t.Error("stdlib error not logging correctly")
}
buf.Reset()
lgr.Errorw("foo bar", Fields{"baz": "qux"})
if !strings.Contains(buf.String(), `[ERROR] foo bar [baz=qux]`) {
t.Log(buf.String())
t.Error("stdlib error not logging correctly")
}
buf.Reset()
assert.PanicsWithValue(t, "[PANIC] test panic", func() {
lgr.Panic("test panic")
})
if !strings.Contains(buf.String(), `test panic`) {
t.Log(buf.String())
t.Error("cli panic not logging correctly")
}
buf.Reset()
assert.PanicsWithValue(t, "[PANIC] Hello World", func() {
lgr.Panicf("Hello %s", "World")
})
if !strings.Contains(buf.String(), `Hello World`) {
t.Log(buf.String())
t.Error("cli panic not logging correctly")
}
buf.Reset()
assert.PanicsWithValue(t, "[PANIC] foo bar [baz=qux]", func() {
lgr.Panicw("foo bar", Fields{"baz": "qux"})
})
if !strings.Contains(buf.String(), `foo bar [baz=qux]`) {
t.Log(buf.String())
t.Error("cli panic not logging correctly")
}
buf.Reset()
// lgr.Fatal("test fatal")
// lgr.Fatalf(template string, args ...interface{})
// lgr.Fatalw(msg string, fields Fields)
}