-
Notifications
You must be signed in to change notification settings - Fork 4
/
presets_test.go
73 lines (68 loc) · 1.71 KB
/
presets_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
package golog
import (
"bytes"
"context"
"io"
"os"
"testing"
)
func TestNewProductionLogger(t *testing.T) {
stdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
t.Cleanup(func() {
os.Stdout = stdout
})
lvl := DEBUG
logger, flusher := NewProductionLogger(lvl)
logger.Info(context.Background(), "hello")
if err := flusher.Flush(); err != nil {
t.Fatalf("could not flush: %s", err)
}
if err := w.Close(); err != nil {
t.Fatalf("could not close: %s", err)
}
output, err := io.ReadAll(r)
if err != nil {
t.Fatalf("could not read: %s", err)
}
if !bytes.Contains(output, []byte(`"level":"INFO"`)) {
t.Fatalf("could not match level key in the logs")
}
if !bytes.Contains(output, []byte(`"stacktrace":[`)) {
t.Fatalf("could not match stactrace key in the logs")
}
if !bytes.Contains(output, []byte(`"timestamp":"`)) {
t.Fatalf("could not match timestamp key in the logs")
}
}
func TestNewDevelopmentLogger(t *testing.T) {
stdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
t.Cleanup(func() {
os.Stdout = stdout
})
lvl := DEBUG
logger, flusher := NewDevelopmentLogger(lvl)
logger.Info(context.Background(), "hello")
if err := flusher.Flush(); err != nil {
t.Fatalf("could not flush: %s", err)
}
if err := w.Close(); err != nil {
t.Fatalf("could not close: %s", err)
}
output, err := io.ReadAll(r)
if err != nil {
t.Fatalf("could not read: %s", err)
}
if !bytes.Contains(output, []byte(`level=`)) {
t.Fatalf("could not match level key in the logs")
}
if !bytes.Contains(output, []byte(`stacktrace=[`)) {
t.Fatalf("could not match stacktrace key in the logs")
}
if !bytes.Contains(output, []byte(`timestamp="`)) {
t.Fatalf("could not match timestamp key in the logs")
}
}