-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlog_test.go
183 lines (161 loc) · 4.24 KB
/
log_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
package logrus
import (
"bytes"
"fmt"
"regexp"
"testing"
"time"
"github.com/sirupsen/logrus"
)
const (
debugMsg = "Debug msg"
infoMsg = "Info msg"
warningMsg = "Warning msg"
errorMsg = "Error msg"
criticalMsg = "Critical msg"
)
func TestNewLogger(t *testing.T) {
levels := []string{"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"}
regexps := []*regexp.Regexp{
regexp.MustCompile(debugMsg),
regexp.MustCompile(infoMsg),
regexp.MustCompile(warningMsg),
regexp.MustCompile(errorMsg),
regexp.MustCompile(criticalMsg),
}
for i, level := range levels {
output, err := logSomeStuff(level)
if err != nil {
t.Error(err)
return
}
time.Sleep(100 * time.Millisecond)
for j := i; j < len(regexps); j++ {
if !regexps[j].MatchString(output) {
t.Errorf("test #%d: The output doesn't contain the expected msg for the level: %s. [%s]", i, level, output)
}
}
}
}
func BenchmarkNewLogger(b *testing.B) {
buff := bytes.NewBuffer(make([]byte, 10*1024*1024))
logger, err := NewLogger(newExtraConfig("DEBUG"), buff)
if err != nil {
b.Error(err.Error())
return
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Debug(debugMsg)
}
}
func BenchmarkNewLogger_parallel(b *testing.B) {
buff := bytes.NewBuffer(make([]byte, 10*1024*1024))
logger, err := NewLogger(newExtraConfig("DEBUG"), buff)
if err != nil {
b.Error(err.Error())
return
}
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.Debug(debugMsg)
}
})
}
func ExampleNewLogger() {
cfg := map[string]interface{}{
Namespace: Config{
Level: "DEBUG",
Module: "pref",
Syslog: false,
StdOut: false,
TextFormatter: &logrus.TextFormatter{
DisableTimestamp: true,
},
},
}
buff := new(bytes.Buffer)
logger, err := NewLogger(cfg, buff)
if err != nil {
fmt.Println(err.Error())
}
logger.Debug(debugMsg)
logger.Info(infoMsg)
logger.Warning(warningMsg)
logger.Error(errorMsg)
logger.Critical(criticalMsg)
fmt.Println(buff.String())
// output:
// level=debug msg="Debug msg" module=pref
// level=info msg="Info msg" module=pref
// level=warning msg="Warning msg" module=pref
// level=error msg="Error msg" module=pref
// level=error msg="Critical msg" module=pref
}
func ExampleWrapLogger() {
buff := new(bytes.Buffer)
logrusLogger := logrus.New()
logrusLogger.Formatter = &logrus.TextFormatter{DisableTimestamp: true}
logrusLogger.SetLevel(logrus.DebugLevel)
logrusLogger.SetOutput(buff)
logger := WrapLogger(logrusLogger, "pref")
logger.Debug(debugMsg)
logger.Info(infoMsg)
logger.Warning(warningMsg)
logger.Error(errorMsg)
logger.Critical(criticalMsg)
fmt.Println(buff.String())
// output:
// level=debug msg="Debug msg" module=pref
// level=info msg="Info msg" module=pref
// level=warning msg="Warning msg" module=pref
// level=error msg="Error msg" module=pref
// level=error msg="Critical msg" module=pref
}
func ExampleConfigGetter() {
cfg := ConfigGetter(newExtraConfig("DEBUG")).(Config)
fmt.Println(cfg.JSONFormatter)
fmt.Printf("%+v\n", cfg.TextFormatter)
// output:
// <nil>
// &{ForceColors:false DisableColors:false DisableTimestamp:false FullTimestamp:true TimestampFormat: DisableSorting:false DisableLevelTruncation:false QuoteEmptyFields:false isTerminal:false FieldMap:map[] Once:{m:{state:0 sema:0} done:0}}
}
func TestNewLogger_unknownLevel(t *testing.T) {
_, err := NewLogger(newExtraConfig("UNKNOWN"), bytes.NewBuffer(make([]byte, 1024)))
if err == nil {
t.Error("The factory didn't return the expected error")
return
}
if err.Error() != "unknown log level: UNKNOWN" {
t.Errorf("The factory didn't return the expected error. Got: %s", err.Error())
}
}
func newExtraConfig(level string) map[string]interface{} {
return map[string]interface{}{
Namespace: Config{
Level: level,
Module: "pref",
Syslog: false,
StdOut: false,
TextFormatter: &logrus.TextFormatter{
FullTimestamp: true,
},
},
}
}
func logSomeStuff(level string) (string, error) {
buff := new(bytes.Buffer)
logger, err := NewLogger(newExtraConfig(level), buff)
if err != nil {
return "", err
}
logger.Debug(debugMsg)
logger.Info(infoMsg)
logger.Warning(warningMsg)
logger.Error(errorMsg)
logger.Critical(criticalMsg)
return buff.String(), nil
}