This repository has been archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_print_test.go
180 lines (150 loc) · 6.71 KB
/
logger_print_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
// nolint: testpackage
package rec
import (
"bytes"
"reflect"
"regexp"
"testing"
)
func TestLogger_Print(t *testing.T) {
t.Parallel()
t.Run("success(Print)", func(t *testing.T) {
t.Parallel()
buf := bytes.NewBuffer(nil)
l := Must(New(buf, WithUseHostnameField(true), WithHostnameFieldKey("host"), WithHostnameFieldValue("test")))
t.Cleanup(buf.Reset)
l.Print(DEFAULT, "test")
actual := buf.String()
expect := regexp.MustCompile(`^{"timestamp":"[0-9]+\-[0-9]+\-[0-9]+T[0-9]+:[0-9]+:[0-9]+[\.0-9]*[0-9Z:\+\-]+","severity":"DEFAULT","host":"test","caller":"[^"]+:[0-9]+","message":"test"}` + defaultLineSeparator)
FailIfNotRegexpMatchString(t, expect, actual)
})
t.Run("success(Default)", func(t *testing.T) {
t.Parallel()
buf := bytes.NewBuffer(nil)
l := Must(New(buf, WithUseHostnameField(true), WithHostnameFieldKey("host"), WithHostnameFieldValue("test")))
t.Cleanup(buf.Reset)
l.Default("test")
actual := buf.String()
expect := regexp.MustCompile(`^{"timestamp":"[0-9]+\-[0-9]+\-[0-9]+T[0-9]+:[0-9]+:[0-9]+[\.0-9]*[0-9Z:\+\-]+","severity":"DEFAULT","host":"test","caller":"[^"]+:[0-9]+","message":"test"}` + defaultLineSeparator)
FailIfNotRegexpMatchString(t, expect, actual)
})
t.Run("success(Debug)", func(t *testing.T) {
t.Parallel()
buf := bytes.NewBuffer(nil)
l := Must(New(buf, WithUseHostnameField(true), WithHostnameFieldKey("host"), WithHostnameFieldValue("test")))
t.Cleanup(buf.Reset)
l.Debug("test")
actual := buf.String()
expect := regexp.MustCompile(`^{"timestamp":"[0-9]+\-[0-9]+\-[0-9]+T[0-9]+:[0-9]+:[0-9]+[\.0-9]*[0-9Z:\+\-]+","severity":"DEBUG","host":"test","caller":"[^"]+:[0-9]+","message":"test"}` + defaultLineSeparator)
FailIfNotRegexpMatchString(t, expect, actual)
})
t.Run("success(Info)", func(t *testing.T) {
t.Parallel()
buf := bytes.NewBuffer(nil)
l := Must(New(buf, WithUseHostnameField(true), WithHostnameFieldKey("host"), WithHostnameFieldValue("test")))
t.Cleanup(buf.Reset)
l.Info("test")
actual := buf.String()
expect := regexp.MustCompile(`^{"timestamp":"[0-9]+\-[0-9]+\-[0-9]+T[0-9]+:[0-9]+:[0-9]+[\.0-9]*[0-9Z:\+\-]+","severity":"INFO","host":"test","caller":"[^"]+:[0-9]+","message":"test"}` + defaultLineSeparator)
FailIfNotRegexpMatchString(t, expect, actual)
})
t.Run("success(Notice)", func(t *testing.T) {
t.Parallel()
buf := bytes.NewBuffer(nil)
l := Must(New(buf, WithUseHostnameField(true), WithHostnameFieldKey("host"), WithHostnameFieldValue("test")))
t.Cleanup(buf.Reset)
l.Notice("test")
actual := buf.String()
expect := regexp.MustCompile(`^{"timestamp":"[0-9]+\-[0-9]+\-[0-9]+T[0-9]+:[0-9]+:[0-9]+[\.0-9]*[0-9Z:\+\-]+","severity":"NOTICE","host":"test","caller":"[^"]+:[0-9]+","message":"test"}` + defaultLineSeparator)
FailIfNotRegexpMatchString(t, expect, actual)
})
t.Run("success(Warning)", func(t *testing.T) {
t.Parallel()
buf := bytes.NewBuffer(nil)
l := Must(New(buf, WithUseHostnameField(true), WithHostnameFieldKey("host"), WithHostnameFieldValue("test")))
t.Cleanup(buf.Reset)
l.Warning("test")
actual := buf.String()
expect := regexp.MustCompile(`^{"timestamp":"[0-9]+\-[0-9]+\-[0-9]+T[0-9]+:[0-9]+:[0-9]+[\.0-9]*[0-9Z:\+\-]+","severity":"WARNING","host":"test","caller":"[^"]+:[0-9]+","message":"test"}` + defaultLineSeparator)
FailIfNotRegexpMatchString(t, expect, actual)
})
t.Run("success(Error)", func(t *testing.T) {
t.Parallel()
buf := bytes.NewBuffer(nil)
l := Must(New(buf, WithUseHostnameField(true), WithHostnameFieldKey("host"), WithHostnameFieldValue("test")))
t.Cleanup(buf.Reset)
l.Error("test")
actual := buf.String()
expect := regexp.MustCompile(`^{"timestamp":"[0-9]+\-[0-9]+\-[0-9]+T[0-9]+:[0-9]+:[0-9]+[\.0-9]*[0-9Z:\+\-]+","severity":"ERROR","host":"test","caller":"[^"]+:[0-9]+","message":"test"}` + defaultLineSeparator)
FailIfNotRegexpMatchString(t, expect, actual)
})
t.Run("success(Critical)", func(t *testing.T) {
t.Parallel()
buf := bytes.NewBuffer(nil)
l := Must(New(buf, WithUseHostnameField(true), WithHostnameFieldKey("host"), WithHostnameFieldValue("test")))
t.Cleanup(buf.Reset)
l.Critical("test")
actual := buf.String()
expect := regexp.MustCompile(`^{"timestamp":"[0-9]+\-[0-9]+\-[0-9]+T[0-9]+:[0-9]+:[0-9]+[\.0-9]*[0-9Z:\+\-]+","severity":"CRITICAL","host":"test","caller":"[^"]+:[0-9]+","message":"test"}` + defaultLineSeparator)
FailIfNotRegexpMatchString(t, expect, actual)
})
t.Run("success(Alert)", func(t *testing.T) {
t.Parallel()
buf := bytes.NewBuffer(nil)
l := Must(New(buf, WithUseHostnameField(true), WithHostnameFieldKey("host"), WithHostnameFieldValue("test")))
t.Cleanup(buf.Reset)
l.Alert("test")
actual := buf.String()
expect := regexp.MustCompile(`^{"timestamp":"[0-9]+\-[0-9]+\-[0-9]+T[0-9]+:[0-9]+:[0-9]+[\.0-9]*[0-9Z:\+\-]+","severity":"ALERT","host":"test","caller":"[^"]+:[0-9]+","message":"test"}` + defaultLineSeparator)
FailIfNotRegexpMatchString(t, expect, actual)
})
t.Run("success(Emergency)", func(t *testing.T) {
t.Parallel()
buf := bytes.NewBuffer(nil)
l := Must(New(buf, WithUseHostnameField(true), WithHostnameFieldKey("host"), WithHostnameFieldValue("test")))
t.Cleanup(buf.Reset)
l.Emergency("test")
actual := buf.String()
expect := regexp.MustCompile(`^{"timestamp":"[0-9]+\-[0-9]+\-[0-9]+T[0-9]+:[0-9]+:[0-9]+[\.0-9]*[0-9Z:\+\-]+","severity":"EMERGENCY","host":"test","caller":"[^"]+:[0-9]+","message":"test"}` + defaultLineSeparator)
FailIfNotRegexpMatchString(t, expect, actual)
})
}
// nolint: paralleltest
func TestLogger_Fatal(t *testing.T) {
backup := exitFn
t.Cleanup(func() { exitFn = backup }) // nolint: paralleltest
exitFn = func(i int) { /* noop */ } // nolint: paralleltest
t.Run("success(Fatal)", func(t *testing.T) {
buf := bytes.NewBuffer(nil)
l := Must(New(buf))
s := EMERGENCY
l.Fatal(s, "test")
expect := regexp.MustCompile(`^{"timestamp":"[0-9T:\.\+\-Z]+","severity":"` + l.uppercase(s) + `","caller":"[^"]+:[0-9]+","message":"test"}` + defaultLineSeparator)
actual := buf.String()
FailIfNotRegexpMatchString(t, expect, actual)
})
}
func TestLogger_Panic(t *testing.T) {
t.Parallel()
t.Run("success(Panic)", func(t *testing.T) {
t.Parallel()
buf := bytes.NewBuffer(nil)
l := Must(New(buf))
const expectMessage = "test"
defer func() {
if err := recover(); err != nil {
actualMessage, ok := err.(string)
if !ok {
FailIfNotEqual(t, "string", reflect.TypeOf(err))
return
}
FailIfNotEqual(t, expectMessage, actualMessage)
}
}()
s := EMERGENCY
l.Panic(s, expectMessage)
expect := regexp.MustCompile(`^{"timestamp":"[0-9T:\.\+\-Z]+","severity":` + l.uppercase(s) + `,"caller":"[^"]+:[0-9]+","message":` + expectMessage + `}` + defaultLineSeparator)
actual := buf.String()
FailIfNotRegexpMatchString(t, expect, actual)
})
}