-
Notifications
You must be signed in to change notification settings - Fork 3
/
event_helper.go
101 lines (87 loc) · 2.44 KB
/
event_helper.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
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package rkquery
import (
rk_logger "github.com/rookie-ninja/rk-logger"
"time"
)
var (
// StdLoggerConfigBytes defines zap logger config whose output path is stdout.
StdLoggerConfigBytes = []byte(`{
"level": "info",
"encoding": "console",
"outputPaths": ["stdout"],
"errorOutputPaths": ["stderr"],
"initialFields": {},
"encoderConfig": {
"messageKey": "msg",
"levelKey": "",
"nameKey": "",
"timeKey": "",
"callerKey": "",
"stacktraceKey": "",
"callstackKey": "",
"errorKey": "",
"timeEncoder": "iso8601",
"fileKey": "",
"levelEncoder": "capital",
"durationEncoder": "second",
"callerEncoder": "full",
"nameEncoder": "full"
},
"maxsize": 1,
"maxage": 7,
"maxbackups": 3,
"localtime": true,
"compress": true
}`)
// StdoutLogger defines zap logger which use StdLoggerConfigBytes as config.
StdoutLogger, _, _ = rk_logger.NewZapLoggerWithBytes(StdLoggerConfigBytes, rk_logger.JSON)
)
// EventHelper is a helper function for easy use of EventData.
type EventHelper struct {
Factory *EventFactory
}
// NewEventHelper creates a new event helper.
func NewEventHelper(factory *EventFactory) *EventHelper {
if factory == nil {
factory = NewEventFactory()
}
return &EventHelper{factory}
}
// Start function creates and start a new event with options.
func (helper *EventHelper) Start(operation string, opts ...EventOption) Event {
event := helper.Factory.CreateEvent(opts...)
event.SetOperation(operation)
event.SetStartTime(time.Now())
return event
}
// Finish current event.
func (helper *EventHelper) Finish(event Event) {
event.SetResCode("OK")
event.SetEndTime(time.Now())
event.Finish()
}
// FinishWithCond finish current event with condition.
func (helper *EventHelper) FinishWithCond(event Event, success bool) {
if success {
event.SetCounter("success", 1)
event.SetResCode("OK")
} else {
event.SetCounter("failure", 1)
event.SetResCode("Fail")
}
event.SetEndTime(time.Now())
event.Finish()
}
// FinishWithError finish current event with error.
func (helper *EventHelper) FinishWithError(event Event, err error) {
if err == nil {
helper.FinishWithCond(event, true)
}
event.SetResCode("Fail")
event.AddErr(err)
helper.FinishWithCond(event, false)
}