-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_test.go
260 lines (212 loc) · 5.7 KB
/
setup_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
package log
import (
"bytes"
"io"
"io/ioutil"
"os"
"strings"
"testing"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func TestGetLoggerDefault(t *testing.T) {
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("failed to open pipe: %v", err)
}
stderr := os.Stderr
os.Stderr = w
defer func() {
os.Stderr = stderr
}()
// Call SetupLogging again so it picks up stderr change
SetupLogging(Config{Stderr: true})
log := getLogger("test")
log.Error("scooby")
w.Close()
buf := &bytes.Buffer{}
if _, err := io.Copy(buf, r); err != nil && err != io.ErrClosedPipe {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(buf.String(), "scooby") {
t.Errorf("got %q, wanted it to contain log output", buf.String())
}
}
func TestLogToFileAndStderr(t *testing.T) {
// setup stderr
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("failed to open pipe: %v", err)
}
stderr := os.Stderr
os.Stderr = w
defer func() {
os.Stderr = stderr
}()
// setup file
logfile, err := ioutil.TempFile("", "go-log-test")
if err != nil {
t.Fatal(err)
}
defer os.Remove(logfile.Name())
os.Setenv(envLoggingFile, logfile.Name())
defer os.Unsetenv(envLoggingFile)
// set log output env var
os.Setenv(envLoggingOutput, "file+stderr")
defer os.Unsetenv(envLoggingOutput)
SetupLogging(configFromEnv())
log := getLogger("test")
want := "scooby"
log.Error(want)
w.Close()
buf := &bytes.Buffer{}
if _, err := io.Copy(buf, r); err != nil && err != io.ErrClosedPipe {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(buf.String(), want) {
t.Errorf("got %q, wanted it to contain log output", buf.String())
}
content, err := ioutil.ReadFile(logfile.Name())
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(content), want) {
t.Logf("want: '%s', got: '%s'", want, string(content))
t.Fail()
}
}
func TestLogToFile(t *testing.T) {
// get tmp log file
logfile, err := ioutil.TempFile("", "go-log-test")
if err != nil {
t.Fatal(err)
}
defer os.Remove(logfile.Name())
// set the go-log file env var
os.Setenv(envLoggingFile, logfile.Name())
defer os.Unsetenv(envLoggingFile)
SetupLogging(configFromEnv())
log := getLogger("test")
// write log to file
want := "grokgrokgrok"
log.Error(want)
// read log file and check contents
content, err := ioutil.ReadFile(logfile.Name())
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(content), want) {
t.Logf("want: '%s', got: '%s'", want, string(content))
t.Fail()
}
}
func TestLogLabels(t *testing.T) {
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("failed to open pipe: %v", err)
}
stderr := os.Stderr
os.Stderr = w
defer func() {
os.Stderr = stderr
}()
// set the go-log labels env var
os.Setenv(envLoggingLabels, "dc=sjc-1,foobar") // foobar to ensure we don't panic on bad input.
defer os.Unsetenv(envLoggingLabels)
SetupLogging(configFromEnv())
log := getLogger("test")
log.Error("scooby")
w.Close()
buf := &bytes.Buffer{}
if _, err := io.Copy(buf, r); err != nil && err != io.ErrClosedPipe {
t.Fatalf("unexpected error: %v", err)
}
t.Log(buf.String())
if !strings.Contains(buf.String(), "{\"dc\": \"sjc-1\"}") {
t.Errorf("got %q, wanted it to contain log output", buf.String())
}
}
func TestSubsystemLevels(t *testing.T) {
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("failed to open pipe: %v", err)
}
stderr := os.Stderr
os.Stderr = w
defer func() {
os.Stderr = stderr
}()
// set the go-log labels env var
os.Setenv(envLogging, "info,test1=debug")
defer os.Unsetenv(envLoggingLabels)
SetupLogging(configFromEnv())
log1 := getLogger("test1")
log2 := getLogger("test2")
log1.Debug("debug1")
log1.Info("info1")
log2.Debug("debug2")
log2.Info("info2")
w.Close()
buf := &bytes.Buffer{}
if _, err := io.Copy(buf, r); err != nil && err != io.ErrClosedPipe {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(buf.String(), "debug1") {
t.Errorf("got %q, wanted it to contain debug1", buf.String())
}
if strings.Contains(buf.String(), "debug2") {
t.Errorf("got %q, wanted it to not contain debug2", buf.String())
}
if !strings.Contains(buf.String(), "info1") {
t.Errorf("got %q, wanted it to contain info1", buf.String())
}
if !strings.Contains(buf.String(), "info2") {
t.Errorf("got %q, wanted it to contain info2", buf.String())
}
}
func TestCustomCore(t *testing.T) {
r1, w1, err := os.Pipe()
if err != nil {
t.Fatalf("failed to open pipe: %v", err)
}
r2, w2, err := os.Pipe()
if err != nil {
t.Fatalf("failed to open pipe: %v", err)
}
// logging should work with the custom core
SetPrimaryCore(newCore(w1, PlaintextOutput, ISO8601TimeFormat, LevelDebug))
log := getLogger("test")
log.Error("scooby")
// SetPrimaryCore should replace the core in previously created loggers
SetPrimaryCore(newCore(w2, PlaintextOutput, ISO8601TimeFormat, LevelDebug))
log.Error("doo")
w1.Close()
w2.Close()
buf1 := &bytes.Buffer{}
buf2 := &bytes.Buffer{}
if _, err := io.Copy(buf1, r1); err != nil && err != io.ErrClosedPipe {
t.Fatalf("unexpected error: %v", err)
}
if _, err := io.Copy(buf2, r2); err != nil && err != io.ErrClosedPipe {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(buf1.String(), "scooby") {
t.Errorf("got %q, wanted it to contain log output", buf1.String())
}
if !strings.Contains(buf2.String(), "doo") {
t.Errorf("got %q, wanted it to contain log output", buf2.String())
}
}
func TestTeeCore(t *testing.T) {
// configure to use a tee logger
tee := zap.New(zapcore.NewTee(
zap.NewNop().Core(),
zap.NewNop().Core(),
), zap.AddCaller())
SetPrimaryCore(tee.Core())
log := getLogger("test")
log.Error("scooby")
// replaces the tee logger with a simple one
SetPrimaryCore(zap.NewNop().Core())
log.Error("doo")
}