-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathphuslog_test.go
98 lines (78 loc) · 1.78 KB
/
phuslog_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
package bench
import (
"io"
"time"
"github.com/phuslu/log"
)
func (u user) MarshalObject(e *log.Entry) {
e.Str("name", u.Name).
Int("age", u.Age).
Time("dob", u.DOB)
}
func phusFields(e *log.Entry) *log.Entry {
e.
Int("bytes", ctxBodyBytes).
Str("request", ctxRequest).
Float64("elapsed_time_ms", ctxTimeElapsedMs).
Object("user", ctxUser).
Time("now", ctxTime).
Strs("months", ctxMonths).
Ints("primes", ctxFirst10Primes).
Objects("users", ctxUsers).
Err(ctxErr)
return e
}
func newPhusLog(w io.Writer) log.Logger {
l := log.Logger{
Level: log.InfoLevel,
Caller: 0,
TimeField: "time",
TimeFormat: time.RFC3339Nano,
Writer: &log.IOWriter{
Writer: w,
},
}
return l
}
type phusLogBench struct {
l log.Logger
}
func (b *phusLogBench) new(w io.Writer) logBenchmark {
return &phusLogBench{
l: newPhusLog(w),
}
}
func (b *phusLogBench) newWithCtx(w io.Writer) logBenchmark {
l := newPhusLog(w)
l.Context = phusFields(log.NewContext(nil)).Value()
return &phusLogBench{
l,
}
}
func (b *phusLogBench) name() string {
return "Phuslog"
}
func (b *phusLogBench) logEvent(msg string) {
b.l.Info().Msg(msg)
}
func (b *phusLogBench) logEventFmt(msg string, args ...any) {
b.l.Info().Msgf(msg, args...)
}
func (b *phusLogBench) logEventCtx(msg string) {
phusFields(b.l.Info()).Msg(msg)
}
func (b *phusLogBench) logEventCtxWeak(msg string) {
b.l.Info().Fields(mapFields()).Msg(msg)
}
func (b *phusLogBench) logDisabled(msg string) {
b.l.Debug().Msg(msg)
}
func (b *phusLogBench) logDisabledFmt(msg string, args ...any) {
b.l.Debug().Msgf(msg, args...)
}
func (b *phusLogBench) logDisabledCtx(msg string) {
phusFields(b.l.Debug()).Msg(msg)
}
func (b *phusLogBench) logDisabledCtxWeak(msg string) {
b.l.Debug().Fields(mapFields()).Msg(msg)
}