-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger_test.go
190 lines (158 loc) · 4.01 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
package log
import (
"errors"
"io"
"io/ioutil"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/upfluence/log/record"
"github.com/upfluence/log/sink/leveled"
"github.com/upfluence/log/sink/writer"
)
func logBenchmark(b *testing.B, fn func(Logger, int), lb loggerBench) {
var l = lb.logger()
b.ResetTimer()
b.Run(lb.bname(), func(b *testing.B) {
for i := 0; i < b.N; i++ {
fn(l, i)
}
})
}
type staticFormatter struct{}
func (staticFormatter) Format(io.Writer, record.Record) error { return nil }
type staticSink struct{}
func (staticSink) Log(record.Record) error { return nil }
type loggerBench interface {
bname() string
logger() Logger
}
type loggerBenchUpf struct {
name string
opts []LoggerOption
}
func (b loggerBenchUpf) bname() string { return "upfluence/log/" + b.name }
func (b loggerBenchUpf) logger() Logger { return NewLogger(b.opts...) }
type loggerBenchLogrus struct {
name string
l logrus.FieldLogger
}
func (b loggerBenchLogrus) bname() string { return "logrus/" + b.name }
func (b loggerBenchLogrus) logger() Logger { return &logrusAdapter{lr: b.l} }
type logrusAdapter struct {
Logger
lr logrus.FieldLogger
}
func (la *logrusAdapter) Info(vs ...interface{}) { la.lr.Info(vs...) }
func (la *logrusAdapter) Infof(fmt string, vs ...interface{}) {
la.lr.Infof(fmt, vs...)
}
func (la *logrusAdapter) WithField(f record.Field) SugaredLogger {
return &logrusAdapter{lr: la.lr.WithField(f.GetKey(), f.GetValue())}
}
var benches = []loggerBench{
loggerBenchUpf{
name: "standard with stack trace",
opts: []LoggerOption{WithSink(writer.NewStandardSink(ioutil.Discard))},
},
loggerBenchLogrus{
name: "standard Text with no stacktrace",
l: &logrus.Logger{
Out: ioutil.Discard,
Formatter: new(logrus.TextFormatter),
Hooks: make(logrus.LevelHooks),
Level: logrus.InfoLevel,
},
},
loggerBenchLogrus{
name: "standard JSON with no stacktrace",
l: &logrus.Logger{
Out: ioutil.Discard,
Formatter: new(logrus.JSONFormatter),
Hooks: make(logrus.LevelHooks),
Level: logrus.InfoLevel,
},
},
loggerBenchLogrus{
name: "leved",
l: &logrus.Logger{
Out: ioutil.Discard,
Formatter: new(logrus.JSONFormatter),
Hooks: make(logrus.LevelHooks),
Level: logrus.WarnLevel,
},
},
loggerBenchUpf{
name: "standard with no stack trace",
opts: []LoggerOption{
WithSink(writer.NewSink(writer.NewFastFormatter(), ioutil.Discard)),
},
},
loggerBenchUpf{
name: "static formatter",
opts: []LoggerOption{
WithSink(writer.NewSink(staticFormatter{}, ioutil.Discard)),
},
},
loggerBenchUpf{
name: "static sink",
opts: []LoggerOption{WithSink(staticSink{})},
},
loggerBenchUpf{
name: "leveled",
opts: []LoggerOption{
WithSink(
leveled.NewSink(record.Notice, writer.NewStandardSink(ioutil.Discard)),
),
},
},
}
func BenchmarkFixedString(b *testing.B) {
for _, bench := range benches {
logBenchmark(
b,
func(l Logger, _ int) { l.Info("foo bar") },
bench,
)
}
}
func BenchmarkFmtString(b *testing.B) {
for _, bench := range benches {
logBenchmark(
b,
func(l Logger, _ int) { l.Infof("foo bar %v", "foo") },
bench,
)
}
}
func BenchmarkWithField(b *testing.B) {
f := Field("foo", "bar")
for _, bench := range benches {
logBenchmark(
b,
func(l Logger, _ int) { l.WithField(f).Info("foo bar") },
bench,
)
}
}
func TestFieldThreshold(t *testing.T) {
var (
s recordSink
l = NewLogger(WithSink(&s), WithDefaultFieldThreshold(record.Notice))
)
l.WithField(Field("foo", 1.)).Debug("foo")
assert.Equal(t, 0, len(s.r.Fields()))
l.WithField(Field("foo", 1)).Notice("bar")
assert.Equal(t, []record.Field{Field("foo", 1)}, s.r.Fields())
}
func TestErrorThreshold(t *testing.T) {
var (
s recordSink
err = errors.New("mock")
l = NewLogger(WithSink(&s), WithDefaultErrorThreshold(record.Info))
)
l.WithError(err).Debug("foo")
assert.Equal(t, 0, len(s.r.Errs()))
l.WithError(err).Info("bar")
assert.Equal(t, []error{err}, s.r.Errs())
}