-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger_test.go
126 lines (109 loc) · 2.6 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
package log_test
import (
"bytes"
"fmt"
"sync"
"testing"
"github.com/caarlos0/log"
)
func TestLoggerOrdering(t *testing.T) {
var l sync.Mutex
var outs [][]byte
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
var out bytes.Buffer
log := log.New(&out)
log.WithError(fmt.Errorf("here")).Info("a")
log.Debug("debug")
log.Debugf("warn %d", 1)
log.Info("info")
log.Infof("warn %d", 1)
log.Warn("warn")
log.Warnf("warn %d", 1)
log.Error("error")
log.Errorf("warn %d", 1)
log.WithField("foo", "bar").Info("foo")
log.IncreasePadding()
log.Info("increased")
log.WithoutPadding().WithField("foo", "bar").Info("without padding")
log.Info("increased")
log.ResetPadding()
l.Lock()
outs = append(outs, out.Bytes())
l.Unlock()
}()
}
wg.Wait()
for i := 0; i < len(outs)-1; i++ {
s1 := string(outs[i])
s2 := string(outs[i+1])
if s1 != s2 {
t.Errorf("at least one of the outputs is different:\n%q\nvs\n%q\n", s1, s2)
}
}
requireEqualOutput(t, outs[0])
}
func TestLogger_printf(t *testing.T) {
var out bytes.Buffer
l := log.New(&out)
l.Infof("logged in %s", "Tobi")
requireEqualOutput(t, out.Bytes())
}
func TestLogger_levels(t *testing.T) {
var out bytes.Buffer
l := log.New(&out)
l.Debug("uploading")
l.Info("upload complete")
requireEqualOutput(t, out.Bytes())
}
func TestLogger_WithField(t *testing.T) {
var out bytes.Buffer
l := log.New(&out)
ctx := l.WithField("file", "sloth.png").WithField("user", "Tobi")
ctx.Debug("uploading")
ctx.Info("upload complete")
requireEqualOutput(t, out.Bytes())
}
func TestLogger_HandlerFunc(t *testing.T) {
var out bytes.Buffer
l := log.New(&out)
l.Infof("logged in %s", "Tobi")
requireEqualOutput(t, out.Bytes())
}
func BenchmarkLogger_small(b *testing.B) {
var out bytes.Buffer
l := log.New(&out)
for i := 0; i < b.N; i++ {
l.Info("login")
}
}
func BenchmarkLogger_medium(b *testing.B) {
var out bytes.Buffer
l := log.New(&out)
for i := 0; i < b.N; i++ {
l.WithField("file", "sloth.png").
WithField("type", "image/png").
WithField("size", 1<<20).
Info("upload")
}
}
func BenchmarkLogger_large(b *testing.B) {
var out bytes.Buffer
l := log.New(&out)
err := fmt.Errorf("boom")
for i := 0; i < b.N; i++ {
l.WithField("file", "sloth.png").
WithField("type", "image/png").
WithField("size", 1<<20).
WithField("some", "more").
WithField("data", "here").
WithField("whatever", "blah blah").
WithField("more", "stuff").
WithField("context", "such useful").
WithField("much", "fun").
WithError(err).Error("upload failed")
}
}