-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_test.go
259 lines (207 loc) · 8.52 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
package gonyan
import (
"os"
"testing"
)
// TestNewLoggerWithStdout will test that the os.Stdout
// file is compatible with current Stream implementation.
// TODO: Capture stdout content to verify correct logging.
func TestNewLoggerWithStdout(t *testing.T) {
l := NewLogger("TestNewLoggerWithStdout", false)
l.RegisterStream(Debug, os.Stdout)
// Expected log is:
// {"tag":"TestNewLoggerWithStdout","message":"this is a debug log and should appear on stdout"}
l.Debug("this is a debug log and should appear on stdout")
}
// TestNewLoggerWithStderr will test that the os.Stdout
// file is compatible with current Stream implementation.
// TODO: Capture stderr content to verify correct logging.
func TestNewLoggerWithStderr(t *testing.T) {
l := NewLogger("TestNewLoggerWithStderr", true)
l.RegisterStream(Error, os.Stderr)
// Expected log is:
// {"tag":"TestNewLoggerWithStderr","message":"this is an error log and should appear on stderr"}
l.Error("this is an error log and should appear on stderr")
}
// TestLoggerSetMetadata verifies that metadata are properly set
// to the internal field.
func TestLoggerSetMetadata(t *testing.T) {
metadata := map[string]string{
"custom": "field",
}
l := NewLogger("TestLoggerSetMetadata", false)
// Set metadata.
l.SetMetadata(metadata)
if l.metadata == nil {
t.Fatal("Metadata is still nil, should have been set!")
}
// Retrieve one metadata field.
val, ok := l.metadata["custom"]
if !ok {
t.Fatal("Metadata with key `custom` not found!")
}
if val != "field" {
t.Fatalf("Unexpected metadata with key `custom` value. Expected: `%s` - Found: `%s`.", "field", val)
}
}
// TestLoggerClearMetadata verifies that metadata field
// is properly set to nil.
func TestLoggerClearMetadata(t *testing.T) {
metadata := map[string]string{
"custom": "field",
}
l := NewLogger("TestLoggerSetMetadata", false)
l.SetMetadata(metadata)
if l.metadata == nil {
t.Fatal("Metadata is still nil, should have been set!")
}
l.ClearMetadata()
if l.metadata != nil {
t.Fatal("Metadata is not nil!")
}
}
// TestLoggerStreamsProperLogDataForDebug verifies that the logger
// sends all information with and without metadata for Debug stream.
func TestLoggerStreamsProperLogDataForDebug(t *testing.T) {
l := NewLogger("TestLoggerStreamsProperLogDataForDebug", false)
stream := newMockStream(1)
l.RegisterStream(Debug, stream)
l.Debugf("Hi %s", "there")
message := <-stream.out
expected := `{"tag":"TestLoggerStreamsProperLogDataForDebug","message":"Hi there"}`
if message != expected {
t.Fatalf("Unexpected message received from stream. Expected: `%s`, found: `%s`", expected, message)
}
l.SetMetadata(map[string]string{"custom": "field"})
l.Debugf("this log should have metadata")
message = <-stream.out
expected = `{"tag":"TestLoggerStreamsProperLogDataForDebug","message":"this log should have metadata","metadata":{"custom":"field"}}`
if message != expected {
t.Fatalf("Unexpected message received from stream. Expected: `%s`, found: `%s`", expected, message)
}
}
// TestLoggerStreamsProperLogDataForVerbose verifies that the logger
// sends all information with and without metadata for Verbose stream.
func TestLoggerStreamsProperLogDataForVerbose(t *testing.T) {
l := NewLogger("TestLoggerStreamsProperLogDataForVerbose", false)
stream := newMockStream(1)
l.RegisterStream(Verbose, stream)
l.Verbose("Hi there")
message := <-stream.out
expected := `{"tag":"TestLoggerStreamsProperLogDataForVerbose","message":"Hi there"}`
if message != expected {
t.Fatalf("Unexpected message received from stream. Expected: `%s`, found: `%s`", expected, message)
}
l.SetMetadata(map[string]string{"custom": "field"})
l.Verbosef("this log should have metadata")
message = <-stream.out
expected = `{"tag":"TestLoggerStreamsProperLogDataForVerbose","message":"this log should have metadata","metadata":{"custom":"field"}}`
if message != expected {
t.Fatalf("Unexpected message received from stream. Expected: `%s`, found: `%s`", expected, message)
}
}
// TestLoggerStreamsProperLogDataForInfo verifies that the logger
// sends all information with and without metadata for Info stream.
func TestLoggerStreamsProperLogDataForInfo(t *testing.T) {
l := NewLogger("TestLoggerStreamsProperLogDataForInfo", false)
stream := newMockStream(1)
l.RegisterStream(Info, stream)
l.Info("Hi there")
message := <-stream.out
expected := `{"tag":"TestLoggerStreamsProperLogDataForInfo","message":"Hi there"}`
if message != expected {
t.Fatalf("Unexpected message received from stream. Expected: `%s`, found: `%s`", expected, message)
}
l.SetMetadata(map[string]string{"custom": "field"})
l.Infof("this log should have metadata")
message = <-stream.out
expected = `{"tag":"TestLoggerStreamsProperLogDataForInfo","message":"this log should have metadata","metadata":{"custom":"field"}}`
if message != expected {
t.Fatalf("Unexpected message received from stream. Expected: `%s`, found: `%s`", expected, message)
}
}
// TestLoggerStreamsProperLogDataForWarning verifies that the logger
// sends all information with and without metadata for Warning stream.
func TestLoggerStreamsProperLogDataForWarning(t *testing.T) {
l := NewLogger("TestLoggerStreamsProperLogDataForWarning", false)
stream := newMockStream(1)
l.RegisterStream(Warning, stream)
l.Warning("Hi there")
message := <-stream.out
expected := `{"tag":"TestLoggerStreamsProperLogDataForWarning","message":"Hi there"}`
if message != expected {
t.Fatalf("Unexpected message received from stream. Expected: `%s`, found: `%s`", expected, message)
}
l.SetMetadata(map[string]string{"custom": "field"})
l.Warningf("this log should have metadata")
message = <-stream.out
expected = `{"tag":"TestLoggerStreamsProperLogDataForWarning","message":"this log should have metadata","metadata":{"custom":"field"}}`
if message != expected {
t.Fatalf("Unexpected message received from stream. Expected: `%s`, found: `%s`", expected, message)
}
}
// TestLoggerStreamsProperLogDataForError verifies that the logger
// sends all information with and without metadata for Error stream.
func TestLoggerStreamsProperLogDataForError(t *testing.T) {
l := NewLogger("TestLoggerStreamsProperLogDataForError", false)
stream := newMockStream(1)
l.RegisterStream(Error, stream)
l.Error("Hi there")
message := <-stream.out
expected := `{"tag":"TestLoggerStreamsProperLogDataForError","message":"Hi there"}`
if message != expected {
t.Fatalf("Unexpected message received from stream. Expected: `%s`, found: `%s`", expected, message)
}
l.SetMetadata(map[string]string{"custom": "field"})
l.Errorf("this log should have metadata")
message = <-stream.out
expected = `{"tag":"TestLoggerStreamsProperLogDataForError","message":"this log should have metadata","metadata":{"custom":"field"}}`
if message != expected {
t.Fatalf("Unexpected message received from stream. Expected: `%s`, found: `%s`", expected, message)
}
}
// TestLoggerStreamsProperLogDataForFatal verifies that the logger
// sends all information with and without metadata for Fatal stream.
func TestLoggerStreamsProperLogDataForFatal(t *testing.T) {
l := NewLogger("TestLoggerStreamsProperLogDataForFatal", false)
stream := newMockStream(1)
l.RegisterStream(Fatal, stream)
l.Fatal("Hi there")
message := <-stream.out
expected := `{"tag":"TestLoggerStreamsProperLogDataForFatal","message":"Hi there"}`
if message != expected {
t.Fatalf("Unexpected message received from stream. Expected: `%s`, found: `%s`", expected, message)
}
l.SetMetadata(map[string]string{"custom": "field"})
l.Fatalf("this log should have metadata")
message = <-stream.out
expected = `{"tag":"TestLoggerStreamsProperLogDataForFatal","message":"this log should have metadata","metadata":{"custom":"field"}}`
if message != expected {
t.Fatalf("Unexpected message received from stream. Expected: `%s`, found: `%s`", expected, message)
}
}
// TestLoggerMutexDisable verifies that the mutex Disable function is properly
// wrapped by the default logger instance.
func TestLoggerMutexDisable(t *testing.T) {
l := NewLogger("", false)
if l.m.disabled != false {
t.Fatalf("Disabled flag should be false!")
}
l.DisableLock()
if l.m.disabled == false {
t.Fatalf("Disabled flag should be true!")
}
}
// TestLoggerMutexReenable verifies that the mutex Enable function is properly
// wrapped by the default logger instance.
func TestLoggerMutexReenable(t *testing.T) {
l := NewLogger("", false)
if l.m.disabled != false {
t.Fatalf("Disabled flag should be false!")
}
l.DisableLock()
l.ReenableLock()
if l.m.disabled != false {
t.Fatalf("Disabled flag should be false!")
}
}