-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
97 lines (78 loc) · 3.13 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
package dat
import (
"log"
"os"
)
// TODO eventreceiver should just be a log interface with a verbose flag
// EventReceiver gets events from dbr methods for logging purposes
type EventReceiver interface {
Event(eventName string)
EventKv(eventName string, kvs map[string]string)
EventErr(eventName string, err error) error
EventErrKv(eventName string, err error, kvs map[string]string) error
Timing(eventName string, nanoseconds int64)
TimingKv(eventName string, nanoseconds int64, kvs map[string]string)
}
type kvs map[string]string
// NullEventReceiver is a sentinel EventReceiver; use it if the caller doesn't supply one
type NullEventReceiver struct{}
// Event receives a simple notification when various events occur
func (n *NullEventReceiver) Event(eventName string) {
}
// EventKv receives a notification when various events occur along with
// optional key/value data
func (n *NullEventReceiver) EventKv(eventName string, kvs map[string]string) {
}
// EventErr receives a notification of an error if one occurs
func (n *NullEventReceiver) EventErr(eventName string, err error) error {
return err
}
// EventErrKv receives a notification of an error if one occurs along with
// optional key/value data
func (n *NullEventReceiver) EventErrKv(eventName string, err error, kvs map[string]string) error {
return err
}
// Timing receives the time an event took to happen
func (n *NullEventReceiver) Timing(eventName string, nanoseconds int64) {
}
// TimingKv receives the time an event took to happen along with optional key/value data
func (n *NullEventReceiver) TimingKv(eventName string, nanoseconds int64, kvs map[string]string) {
}
// LogEventReceiver is a sentinel EventReceiver; use it if the caller doesn't supply one
type LogEventReceiver struct {
log *log.Logger
}
// NewLogEventReceiver creates a new LogEventReceiver.
func NewLogEventReceiver(prefix string) *LogEventReceiver {
return &LogEventReceiver{
log: log.New(os.Stdout, prefix, 0),
}
}
// Event receives a simple notification when various events occur
func (ler *LogEventReceiver) Event(eventName string) {
ler.log.Println(eventName)
}
// EventKv receives a notification when various events occur along with
// optional key/value data
func (ler *LogEventReceiver) EventKv(eventName string, kvs map[string]string) {
ler.log.Println(eventName, kvs)
}
// EventErr receives a notification of an error if one occurs
func (ler *LogEventReceiver) EventErr(eventName string, err error) error {
ler.log.Println(eventName, err)
return err
}
// EventErrKv receives a notification of an error if one occurs along with
// optional key/value data
func (ler *LogEventReceiver) EventErrKv(eventName string, err error, kvs map[string]string) error {
ler.log.Println(eventName, err, kvs)
return err
}
// Timing receives the time an event took to happen
func (ler *LogEventReceiver) Timing(eventName string, nanoseconds int64) {
ler.log.Println(eventName, nanoseconds)
}
// TimingKv receives the time an event took to happen along with optional key/value data
func (ler *LogEventReceiver) TimingKv(eventName string, nanoseconds int64, kvs map[string]string) {
ler.log.Println(eventName, nanoseconds, kvs)
}