-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevent.go
120 lines (103 loc) · 2.55 KB
/
event.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
package logger
import (
"sync"
"time"
"github.com/hamba/logger/v2/internal/bytes"
)
var eventPool = &sync.Pool{
New: func() interface{} {
return &Event{
buf: bytes.NewBuffer(512),
}
},
}
// Event is a log event.
type Event struct {
fmtr Formatter
buf *bytes.Buffer
}
func newEvent(fmtr Formatter) *Event {
e := eventPool.Get().(*Event)
e.fmtr = fmtr
e.buf.Reset()
return e
}
func putEvent(e *Event) {
eventPool.Put(e)
}
// AppendString appends a string to the event.
func (e *Event) AppendString(k, s string) {
e.fmtr.AppendKey(e.buf, k)
e.fmtr.AppendString(e.buf, s)
}
// AppendStrings appends strings to the event.
func (e *Event) AppendStrings(k string, s []string) {
e.fmtr.AppendKey(e.buf, k)
e.fmtr.AppendArrayStart(e.buf)
for i, ss := range s {
if i > 0 {
e.fmtr.AppendArraySep(e.buf)
}
e.fmtr.AppendString(e.buf, ss)
}
e.fmtr.AppendArrayEnd(e.buf)
}
// AppendBytes appends bytes to the event.
func (e *Event) AppendBytes(k string, p []byte) {
e.fmtr.AppendKey(e.buf, k)
e.fmtr.AppendArrayStart(e.buf)
for i, b := range p {
if i > 0 {
e.fmtr.AppendArraySep(e.buf)
}
e.fmtr.AppendInt(e.buf, int64(b))
}
e.fmtr.AppendArrayEnd(e.buf)
}
// AppendBool appends a bool to the event.
func (e *Event) AppendBool(k string, b bool) {
e.fmtr.AppendKey(e.buf, k)
e.fmtr.AppendBool(e.buf, b)
}
// AppendInt appends an int to the event.
func (e *Event) AppendInt(k string, i int64) {
e.fmtr.AppendKey(e.buf, k)
e.fmtr.AppendInt(e.buf, i)
}
// AppendInts appends ints to the event.
func (e *Event) AppendInts(k string, a []int) {
e.fmtr.AppendKey(e.buf, k)
e.fmtr.AppendArrayStart(e.buf)
for i, ii := range a {
if i > 0 {
e.fmtr.AppendArraySep(e.buf)
}
e.fmtr.AppendInt(e.buf, int64(ii))
}
e.fmtr.AppendArrayEnd(e.buf)
}
// AppendUint appends a uint to the event.
func (e *Event) AppendUint(k string, i uint64) {
e.fmtr.AppendKey(e.buf, k)
e.fmtr.AppendUint(e.buf, i)
}
// AppendFloat appends a float to the event.
func (e *Event) AppendFloat(k string, f float64) {
e.fmtr.AppendKey(e.buf, k)
e.fmtr.AppendFloat(e.buf, f)
}
// AppendTime appends a time to the event.
func (e *Event) AppendTime(k string, d time.Time) {
e.fmtr.AppendKey(e.buf, k)
e.fmtr.AppendTime(e.buf, d)
}
// AppendDuration appends a duration to the event.
func (e *Event) AppendDuration(k string, d time.Duration) {
e.fmtr.AppendKey(e.buf, k)
e.fmtr.AppendDuration(e.buf, d)
}
// AppendInterface appends a interface to the event.
func (e *Event) AppendInterface(k string, v interface{}) {
e.fmtr.AppendKey(e.buf, k)
e.fmtr.AppendInterface(e.buf, v)
}