-
Notifications
You must be signed in to change notification settings - Fork 0
/
wd.go
112 lines (89 loc) · 2.83 KB
/
wd.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
package wd
import (
"github.com/mono83/slf"
"time"
)
// Watchdog interface combines logger and metrics functionality
type Watchdog interface {
slf.Logger
slf.StatsReporter
// WithParams returns copy of watchdog with pre-setted params
// Useful for context-based logging
WithParams(...slf.Param) Watchdog
}
// New builds and returns new watchdog
func New(name, metricsPrefix string) Watchdog {
return Custom(name, metricsPrefix, stdDispatcher)
}
// NewLogger builds and returns new watchdog, supposed to be used as logger
// i.e. without metrics prefix
func NewLogger(name string) Watchdog {
return New(name, "")
}
// NewStatsReporter builds and returns new watchdog, supposed to be used as stats reporter
func NewStatsReporter(metricsPrefix string) Watchdog {
return New("", metricsPrefix)
}
// Custom creates and returns watchdog with custom events receivers pipe
func Custom(name, metricsPrefix string, target slf.Receiver) Watchdog {
return &watchdog{
name: name,
prefix: metricsPrefix,
pipe: target.Receive,
}
}
type watchdog struct {
name, prefix string
params []slf.Param
pipe func(slf.Event)
}
func (w watchdog) join(p []slf.Param) []slf.Param {
if len(w.params) == 0 {
return p
}
return append(w.params, p...)
}
func (w watchdog) Trace(message string, p ...slf.Param) {
w.pipe(newLog(slf.TypeTrace, w.name, message, w.join(p)))
}
func (w watchdog) Debug(message string, p ...slf.Param) {
w.pipe(newLog(slf.TypeDebug, w.name, message, w.join(p)))
}
func (w watchdog) Info(message string, p ...slf.Param) {
w.pipe(newLog(slf.TypeInfo, w.name, message, w.join(p)))
}
func (w watchdog) Warning(message string, p ...slf.Param) {
w.pipe(newLog(slf.TypeWarning, w.name, message, w.join(p)))
}
func (w watchdog) Error(message string, p ...slf.Param) {
w.pipe(newLog(slf.TypeError, w.name, message, w.join(p)))
}
func (w watchdog) Alert(message string, p ...slf.Param) {
w.pipe(newLog(slf.TypeAlert, w.name, message, w.join(p)))
}
func (w watchdog) Emergency(message string, p ...slf.Param) {
w.pipe(newLog(slf.TypeEmergency, w.name, message, w.join(p)))
}
func (w watchdog) IncCounter(name string, value int64, p ...slf.Param) {
w.pipe(newMetrics(slf.TypeInc, w.name, w.prefix+name, value, w.join(p)))
}
func (w watchdog) UpdateGauge(name string, value int64, p ...slf.Param) {
w.pipe(newMetrics(slf.TypeGauge, w.name, w.prefix+name, value, w.join(p)))
}
func (w watchdog) RecordTimer(name string, d time.Duration, p ...slf.Param) {
w.pipe(newMetrics(slf.TypeDuration, w.name, w.prefix+name, int64(d), w.join(p)))
}
func (w watchdog) Timer(name string, p ...slf.Param) slf.Timer {
return slf.NewTimer(name, w.join(p), w)
}
func (w watchdog) WithParams(p ...slf.Param) Watchdog {
if len(p) == 0 {
return w
}
return &watchdog{
name: w.name,
prefix: w.prefix,
pipe: w.pipe,
params: append(w.params, p...),
}
}