-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_batch.go
149 lines (121 loc) · 3.02 KB
/
event_batch.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package batchlog
import (
"encoding/json"
"fmt"
"strconv"
"github.com/rs/zerolog"
)
type pairString struct {
key string
value string
}
// event is based on zerolog.Event, so dont reuse this after logging
type event struct {
logger *Logger
level zerolog.Level
event *zerolog.Event
done bool
batchKeysM map[string]bool // map of keys that need to batched
batchKeysA []string // array of keys that need to batched
group pairString
}
func newRawEvent(l *Logger, e func() *zerolog.Event, lvl zerolog.Level) *event {
return &event{
logger: l,
level: lvl,
event: e(),
// groupKey: "",
batchKeysM: make(map[string]bool),
batchKeysA: make([]string, 0),
}
}
// BatchMsg will batch the message (string) and logging out
// this equal: e.BatchStr("message", msg) + e.Send()
//
// NOTICE: once this method is called, the *event should be disposed.
// Calling Msg twice can have unexpected result.
func (e *event) BatchMsg(msg string) {
if e.done {
return
}
e.done = true
if len(msg) > 0 {
e.BatchStr("message", msg)
}
batcher.Batch(e)
}
// Msg sends the *event with msg added as the message field if not empty.
// it will processed as async batch operation if any param of it is batched
// otherwise it just prints out immediately
//
// NOTICE: once this method is called, the *event should be disposed.
// Calling Msg twice can have unexpected result.
func (e *event) Msg(msg string) {
if e.done {
return
}
e.done = true
batcher.Batch(e.Str("message", msg))
}
// Send is equivalent to calling Msg("").
//
// NOTICE: once this method is called, the *Event should be disposed.
func (e *event) Send() {
if e.done {
return
}
e.done = true
batcher.Batch(e)
}
func (e *event) BatchBool(key string, value bool) *event {
if e.Bool(key, value) == nil {
return nil
}
return e.batch(key, strconv.FormatBool(value))
}
func (e *event) BatchInt(key string, value int) *event {
if e.Int(key, value) == nil {
return nil
}
return e.batch(key, strconv.FormatInt(int64(value), 10))
}
func (e *event) BatchFloat32(key string, value float32) *event {
if e.Float32(key, value) == nil {
return nil
}
return e.batch(key, strconv.FormatFloat(float64(value), 'f', -1, 32))
}
func (e *event) BatchFloat64(key string, value float64) *event {
if e.Float64(key, value) == nil {
return nil
}
return e.batch(key, strconv.FormatFloat(float64(value), 'f', -1, 64))
}
func (e *event) BatchAny(key string, i interface{}) *event {
if e.Any(key, i) == nil {
return nil
}
if b, err := json.Marshal(i); err != nil {
return nil
} else {
return e.batch(key, string(b))
}
}
func (e *event) BatchStr(key string, value string) *event {
if e.Str(key, value) == nil {
return nil
}
return e.batch(key, value)
}
func (e *event) BatchErr(err error) *event {
if e.Err(err) == nil {
return nil
}
return e.batch("error", err.Error())
}
func (e *event) batch(key string, value string) *event {
realKey := fmt.Sprintf("%s_%s", key, value)
e.batchKeysM[realKey] = true
e.batchKeysA = append(e.batchKeysA, realKey)
return e
}