Skip to content

Commit 67b0a86

Browse files
author
Aiden
committed
add group feature
1 parent 4a195e2 commit 67b0a86

File tree

4 files changed

+116
-5
lines changed

4 files changed

+116
-5
lines changed

batcher.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,25 @@ type chainData struct {
4848
timeout time.Duration // option
4949

5050
lastUpdated time.Time
51-
wait time.Duration
51+
wait time.Duration // option
52+
53+
groupKey string
54+
groupValues []string
5255
}
5356

57+
// type marshalZerologTexts []text
58+
// type text string
59+
60+
// func (uu marshalZerologTexts) MarshalZerologArray(a *zerolog.Array) {
61+
// for _, u := range uu {
62+
// a.Object(u)
63+
// }
64+
// }
65+
66+
// func (u text) MarshalZerologObject(e *zerolog.Event) {
67+
// e.Str("", string(u))
68+
// }
69+
5470
func (c *chainData) needLogged() bool {
5571
now := time.Now()
5672
return c.count >= c.maxRelativeBatch || // reached max batch counts
@@ -125,7 +141,7 @@ func (b *chainedBatcher) Batch(e *event, opts ...BatchOption) {
125141
if k == len(e.batchKeysA)-1 {
126142
value, _ := node.nexts[v]
127143
if value.data == nil {
128-
value.data = rawChainData(e.event)
144+
value.data = rawChainData(e)
129145

130146
if len(e.logger.opts) > 0 {
131147
value.data.applyOpts(e.logger.opts...)
@@ -136,21 +152,29 @@ func (b *chainedBatcher) Batch(e *event, opts ...BatchOption) {
136152

137153
value.data.count++
138154
value.data.lastUpdated = time.Now()
155+
156+
if len(e.group.key) > 0 {
157+
value.data.groupValues = append(value.data.groupValues, e.group.value)
158+
}
159+
139160
node.nexts[v] = value
140161
}
141162

142163
node = node.nexts[v]
143164
}
144165
}
145166

146-
func rawChainData(event *zerolog.Event) *chainData {
167+
func rawChainData(event *event) *chainData {
147168
return &chainData{
148-
event: event,
169+
event: event.event,
149170
count: 0,
150171
start: time.Now(),
151172
timeout: 10 * time.Second,
152173
maxRelativeBatch: 20,
153174
wait: 5 * time.Second,
175+
// lastUpdated: time.Now(),
176+
groupKey: event.group.key,
177+
// groupValues: make([]string, 0),
154178
}
155179
}
156180

@@ -180,6 +204,11 @@ func (b *chainedBatcher) logging() {
180204
// and remove that element out of logger
181205
func batchOut(b *chainedBatcher, c *chainData, i int) {
182206
c.event.Int("__repeat", c.count)
207+
208+
if len(c.groupValues) > 0 {
209+
c.event.Strs(c.groupKey, c.groupValues)
210+
}
211+
183212
c.event.Send()
184213

185214
b.logged[i].clean()

event_batch.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,23 @@ import (
88
"github.com/rs/zerolog"
99
)
1010

11+
type pairString struct {
12+
key string
13+
value string
14+
}
15+
1116
// event is based on zerolog.Event, so dont reuse this after logging
1217
type event struct {
1318
logger *Logger
1419

1520
level zerolog.Level
1621
event *zerolog.Event
1722
done bool
18-
// groupKey string
1923

2024
batchKeysM map[string]bool // map of keys that need to batched
2125
batchKeysA []string // array of keys that need to batched
26+
27+
group pairString
2228
}
2329

2430
func newRawEvent(l *Logger, e func() *zerolog.Event, lvl zerolog.Level) *event {

event_group.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package batchlog
2+
3+
import (
4+
"encoding/json"
5+
"strconv"
6+
)
7+
8+
func (e *event) GroupBool(key string, value bool) *event {
9+
e.group = pairString{
10+
key: key,
11+
value: strconv.FormatBool(value),
12+
}
13+
return e
14+
}
15+
16+
func (e *event) GroupInt(key string, value int) *event {
17+
e.group = pairString{
18+
key: key,
19+
value: strconv.FormatInt(int64(value), 10),
20+
}
21+
return e
22+
}
23+
24+
func (e *event) GroupFloat32(key string, value float32) *event {
25+
e.group = pairString{
26+
key: key,
27+
value: strconv.FormatFloat(float64(value), 'f', -1, 32),
28+
}
29+
return e
30+
}
31+
32+
func (e *event) GroupFloat64(key string, value float64) *event {
33+
e.group = pairString{
34+
key: key,
35+
value: strconv.FormatFloat(float64(value), 'f', -1, 64),
36+
}
37+
return e
38+
}
39+
40+
func (e *event) GroupInterface(key string, i interface{}) *event {
41+
if b, err := json.Marshal(i); err != nil {
42+
return nil
43+
} else {
44+
e.group = pairString{
45+
key: key,
46+
value: string(b),
47+
}
48+
return e
49+
}
50+
}
51+
52+
func (e *event) GroupStr(key string, value string) *event {
53+
e.group = pairString{
54+
key: key,
55+
value: value,
56+
}
57+
return e
58+
}
59+
60+
func (e *event) GroupErr(err error) *event {
61+
e.group = pairString{
62+
key: "error",
63+
value: err.Error(),
64+
}
65+
return e
66+
}

logtest/log_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ func TestMaxRelatitveBatch(t *testing.T) {
5656
time.Sleep(time.Hour)
5757
}
5858

59+
func TestGroup(t *testing.T) {
60+
logger := batchlog.NewLogger(batchlog.OptTimeout(time.Hour))
61+
for i := 0; i < 30; i++ {
62+
time.Sleep(1500 * time.Millisecond)
63+
logger.Debug().BatchStr("tokenId", "123456").GroupInt("id", i).BatchMsg("hello")
64+
}
65+
fmt.Println("done")
66+
time.Sleep(time.Hour)
67+
}
68+
5969
func TestScenario(t *testing.T) {
6070
logger := batchlog.NewLogger(batchlog.OptTimeout(time.Hour))
6171

0 commit comments

Comments
 (0)