-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonlog_test.go
236 lines (226 loc) · 6.19 KB
/
jsonlog_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
package log
import (
"bytes"
golog "log"
"strings"
"testing"
)
func TestNewLogger(t *testing.T) {
// Test default configuration
defaultLogger := defaultJSONLogger()
if defaultLogger == nil {
t.Error("Expected non-nil logger, got nil")
}
// Test custom configuration
prefix := "test"
verbosity := DEBUG
cfg := &LoggerConfiguration{Prefix: prefix, Verbosity: verbosity}
logger := newLogger(cfg)
if logger == nil {
t.Error("Expected non-nil logger, got nil")
}
if logger.prefix != prefix {
t.Errorf("Expected prefix %q, got %q", prefix, logger.prefix)
}
if logger.verbosity != verbosity {
t.Errorf("Expected verbosity %d, got %d", verbosity, logger.verbosity)
}
}
func TestShutdown(t *testing.T) {
logger := defaultJSONLogger()
err := logger.Shutdown()
if err != nil {
t.Errorf("Expected nil error, got %v", err)
}
}
func TestSetVerbosity(t *testing.T) {
logger := defaultJSONLogger()
logger.SetVerbosity("debug")
if logger.verbosity != DEBUG {
t.Errorf("Expected verbosity %d, got %d", DEBUG, logger.verbosity)
}
logger.SetVerbosity("info")
if logger.verbosity != INFO {
t.Errorf("Expected verbosity %d, got %d", INFO, logger.verbosity)
}
logger.SetVerbosity("warn")
if logger.verbosity != WARN {
t.Errorf("Expected verbosity %d, got %d", WARN, logger.verbosity)
}
logger.SetVerbosity("err")
if logger.verbosity != ERR {
t.Errorf("Expected verbosity %d, got %d", ERR, logger.verbosity)
}
logger.SetVerbosity("fatal")
if logger.verbosity != FATAL {
t.Errorf("Expected verbosity %d, got %d", FATAL, logger.verbosity)
}
logger.SetVerbosity("invalid")
if logger.verbosity != WARN {
t.Errorf("Expected verbosity %d, got %d", WARN, logger.verbosity)
}
}
func TestDebug(t *testing.T) {
buf := &bytes.Buffer{}
bufErr := &bytes.Buffer{}
logger := &logger{out: golog.New(buf, "", 0), err: golog.New(bufErr, "", 0)}
logger.SetVerbosity("debug")
logger.Debug("test")
if !strings.Contains(buf.String(), "debug") {
t.Error("Expected log message to contain 'debug'")
}
if !strings.Contains(buf.String(), "test") {
t.Error("Expected log message to contain 'test'")
}
if bufErr.String() != "" {
t.Error("Expected empty log message in error output, got", buf.String())
}
buf.Reset()
logger.verbosity = INFO
logger.Debug("test")
if buf.String() != "" {
t.Error("Expected empty log message, got", buf.String())
}
}
func TestInfo(t *testing.T) {
buf := &bytes.Buffer{}
bufErr := &bytes.Buffer{}
logger := &logger{out: golog.New(buf, "", 0), err: golog.New(bufErr, "", 0)}
logger.verbosity = INFO
logger.Info("test")
if !strings.Contains(buf.String(), "info") {
t.Error("Expected log message to contain 'info'")
}
if !strings.Contains(buf.String(), "test") {
t.Error("Expected log message to contain 'test'")
}
buf.Reset()
logger.Debug("test")
if buf.String() != "" {
t.Error("Expected empty log message, got", buf.String())
}
buf.Reset()
if bufErr.String() != "" {
t.Error("Expected empty log message in error output, got", buf.String())
}
logger.verbosity = WARN
logger.Info("test")
if buf.String() != "" {
t.Error("Expected empty log message, got", buf.String())
}
}
func TestWarn(t *testing.T) {
buf := &bytes.Buffer{}
bufErr := &bytes.Buffer{}
logger := &logger{out: golog.New(buf, "", 0), err: golog.New(bufErr, "", 0)}
logger.verbosity = WARN
logger.Warn("test")
if !strings.Contains(buf.String(), "warn") {
t.Error("Expected log message to contain 'warn'")
}
if !strings.Contains(buf.String(), "test") {
t.Error("Expected log message to contain 'test'")
}
buf.Reset()
logger.Err("test")
if !strings.Contains(bufErr.String(), "error") {
t.Error("Expected log message to contain 'error'")
}
if !strings.Contains(bufErr.String(), "test") {
t.Error("Expected log message to contain 'test'")
}
bufErr.Reset()
logger.Debug("test")
if buf.String() != "" {
t.Error("Expected empty log message, got", buf.String())
}
buf.Reset()
logger.Info("test")
if buf.String() != "" {
t.Error("Expected empty log message, got", buf.String())
}
buf.Reset()
if bufErr.String() != "" {
t.Error("Expected empty log message in error output, got", bufErr.String())
}
logger.verbosity = ERR
logger.Warn("test")
if buf.String() != "" {
t.Error("Expected empty log message, got", buf.String())
}
}
func TestErr(t *testing.T) {
buf := &bytes.Buffer{}
bufErr := &bytes.Buffer{}
logger := &logger{out: golog.New(buf, "", 0), err: golog.New(bufErr, "", 0)}
logger.verbosity = ERR
logger.Err("test")
if !strings.Contains(bufErr.String(), "error") {
t.Error("Expected log message to contain 'error'")
}
if !strings.Contains(bufErr.String(), "test") {
t.Error("Expected log message to contain 'test'")
}
if buf.String() != "" {
t.Error("Expected empty log message, got", buf.String())
}
bufErr.Reset()
logger.Debug("test")
if buf.String() != "" {
t.Error("Expected empty log message, got", buf.String())
}
buf.Reset()
logger.Info("test")
if buf.String() != "" {
t.Error("Expected empty log message, got", buf.String())
}
buf.Reset()
logger.Warn("test")
if buf.String() != "" {
t.Error("Expected empty log message, got", buf.String())
}
buf.Reset()
logger.verbosity = FATAL
logger.Err("test")
if buf.String() != "" {
t.Error("Expected empty log message, got", buf.String())
}
}
func TestPanic(t *testing.T) {
buf := &bytes.Buffer{}
bufErr := &bytes.Buffer{}
logger := &logger{out: golog.New(buf, "", 0), err: golog.New(bufErr, "", 0)}
logger.verbosity = FATAL
logger.Debug("test")
if buf.String() != "" {
t.Error("Expected empty log message, got", buf.String())
}
buf.Reset()
logger.Info("test")
if buf.String() != "" {
t.Error("Expected empty log message, got", buf.String())
}
buf.Reset()
logger.Warn("test")
if buf.String() != "" {
t.Error("Expected empty log message, got", buf.String())
}
buf.Reset()
logger.Err("test")
if buf.String() != "" {
t.Error("Expected empty log message, got", buf.String())
}
buf.Reset()
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic")
}
if !strings.Contains(bufErr.String(), "panic") {
t.Error("Expected log message to contain 'panic'")
}
if !strings.Contains(bufErr.String(), "test") {
t.Error("Expected log message to contain 'test'")
}
}()
logger.Panic("test")
}