forked from mailgun/groupcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_test.go
77 lines (72 loc) · 1.91 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
package groupcache
import (
"bytes"
"errors"
"github.com/sirupsen/logrus"
"testing"
)
// This tests the compatibility of the LogrusLogger with the previous behavior.
func TestLogrusLogger(t *testing.T) {
var buf bytes.Buffer
l := logrus.New()
l.SetFormatter(&logrus.TextFormatter{
DisableTimestamp: true,
})
l.Out = &buf
e := logrus.NewEntry(l)
e = e.WithField("ContextKey", "ContextVal")
SetLogger(e)
logger.Error().
WithFields(map[string]interface{}{
"err": errors.New("test error"),
"key": "keyValue",
"category": "groupcache",
}).Printf("error retrieving key from peer %s", "http://127.0.0.1:8080")
interfaceOut := buf.String()
buf.Reset()
e.WithFields(logrus.Fields{
"err": errors.New("test error"),
"key": "keyValue",
"category": "groupcache",
}).Errorf("error retrieving key from peer %s", "http://127.0.0.1:8080")
logrusOut := buf.String()
if interfaceOut != logrusOut {
t.Errorf("output is not the same.\ngot:\n%s\nwant:\n%s", interfaceOut, logrusOut)
}
}
func BenchmarkLogrusLogger(b *testing.B) {
var buf bytes.Buffer
l := logrus.New()
l.SetFormatter(&logrus.TextFormatter{
DisableTimestamp: true,
})
l.Out = &buf
e := logrus.NewEntry(l)
SetLogger(e)
for i := 0; i < b.N; i++ {
logger.Error().
WithFields(map[string]interface{}{
"err": errors.New("test error"),
"key": "keyValue",
"category": "groupcache",
}).Printf("error retrieving key from peer %s", "http://127.0.0.1:8080")
buf.Reset()
}
}
func BenchmarkLogrus(b *testing.B) {
var buf bytes.Buffer
l := logrus.New()
l.SetFormatter(&logrus.TextFormatter{
DisableTimestamp: true,
})
l.Out = &buf
e := logrus.NewEntry(l)
for i := 0; i < b.N; i++ {
e.WithFields(logrus.Fields{
"err": errors.New("test error"),
"key": "keyValue",
"category": "groupcache",
}).Errorf("error retrieving key from peer %s", "http://127.0.0.1:8080")
buf.Reset()
}
}