-
Notifications
You must be signed in to change notification settings - Fork 5
/
zerolog_test.go
107 lines (86 loc) · 2.18 KB
/
zerolog_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
package bench
import (
"io"
"time"
"github.com/rs/zerolog"
)
func (u user) MarshalZerologObject(e *zerolog.Event) {
e.Str("name", u.Name).
Int("age", u.Age).
Time("dob", u.DOB)
}
func (uu users) MarshalZerologArray(a *zerolog.Array) {
for _, u := range uu {
a.Object(u)
}
}
func zerologFields(e *zerolog.Event) *zerolog.Event {
e.
Int("bytes", ctxBodyBytes).
Str("request", ctxRequest).
Float64("elapsed_time_ms", ctxTimeElapsedMs).
Object("user", ctxUser).
Time("now", ctxTime).
Strs("months", ctxMonths).
Ints("primes", ctxFirst10Primes).
Array("users", ctxUsers).
Err(ctxErr)
return e
}
func zerologCtx(c zerolog.Context) zerolog.Context {
c.
Int("bytes", ctxBodyBytes).
Str("request", ctxRequest).
Float64("elapsed_time_ms", ctxTimeElapsedMs).
Object("user", ctxUser).
Time("now", ctxTime).
Strs("months", ctxMonths).
Ints("primes", ctxFirst10Primes).
Array("users", ctxUsers).
Err(ctxErr)
return c
}
func newZerolog(w io.Writer) zerolog.Logger {
zerolog.TimeFieldFormat = time.RFC3339Nano
return zerolog.New(w).Level(zerolog.InfoLevel).With().Timestamp().Logger()
}
type zerologBench struct {
l zerolog.Logger
}
func (b *zerologBench) new(w io.Writer) logBenchmark {
return &zerologBench{
l: newZerolog(w),
}
}
func (b *zerologBench) newWithCtx(w io.Writer) logBenchmark {
return &zerologBench{
l: zerologCtx(newZerolog(w).With()).Logger(),
}
}
func (b *zerologBench) name() string {
return "Zerolog"
}
func (b *zerologBench) logEvent(msg string) {
b.l.Info().Msg(msg)
}
func (b *zerologBench) logEventFmt(msg string, args ...any) {
b.l.Info().Msgf(msg, args...)
}
func (b *zerologBench) logEventCtx(msg string) {
zerologFields(b.l.Info()).Msg(msg)
}
func (b *zerologBench) logEventCtxWeak(msg string) {
b.l.Info().Fields(alternatingKeyValuePairs()).Msg(msg)
}
func (b *zerologBench) logDisabled(msg string) {
b.l.Debug().Msg(msg)
}
func (b *zerologBench) logDisabledFmt(msg string, args ...any) {
b.l.Debug().Msgf(msg, args...)
}
func (b *zerologBench) logDisabledCtx(msg string) {
zerologFields(b.l.Debug()).Msg(msg)
}
func (b *zerologBench) logDisabledCtxWeak(msg string) {
b.l.Debug().Fields(alternatingKeyValuePairs()).Msg(msg)
}