-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
188 lines (151 loc) · 3.65 KB
/
handler.go
File metadata and controls
188 lines (151 loc) · 3.65 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package logged
import (
"io"
"sync"
"time"
)
// Handler represents a log handler.
type Handler interface {
// Log write the log message.
Log(msg string, lvl Level, ctx []interface{})
}
// HandlerFunc is a function handler.
type HandlerFunc func(msg string, lvl Level, ctx []interface{})
// Log write the log message.
func (h HandlerFunc) Log(msg string, lvl Level, ctx []interface{}) {
h(msg, lvl, ctx)
}
type bufStreamHandler struct {
flushBytes int
flushInterval time.Duration
w io.Writer
fmtr Formatter
mx sync.Mutex
pool pool
buf *buffer
ch chan *buffer
shutdown chan bool
}
// BufferedStreamHandler writes buffered log messages to an io.Writer with the given format.
func BufferedStreamHandler(w io.Writer, flushBytes int, flushInterval time.Duration, fmtr Formatter) Handler {
pool := newPool(flushBytes)
h := &bufStreamHandler{
flushBytes: flushBytes,
flushInterval: flushInterval,
fmtr: fmtr,
w: w,
pool: pool,
buf: pool.Get(),
ch: make(chan *buffer, 32),
shutdown: make(chan bool, 1),
}
go h.run()
return h
}
func (h *bufStreamHandler) run() {
doneChan := make(chan bool)
go func() {
for buf := range h.ch {
h.w.Write(buf.Bytes())
h.pool.Put(buf)
}
doneChan <- true
}()
ticker := time.NewTicker(h.flushInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
h.withBufferLock(func() {
h.swap()
})
case <-doneChan:
h.shutdown <- true
return
}
}
}
// Log write the log message.
func (h *bufStreamHandler) Log(msg string, lvl Level, ctx []interface{}) {
h.withBufferLock(func() {
// Dont write to a closed
if h.buf == nil {
return
}
h.buf.Write(h.fmtr.Format(msg, lvl, ctx))
if h.buf.Len() >= h.flushBytes {
h.swap()
}
})
}
// Close closes the handler, waiting for all buffers to be flushed.
func (h *bufStreamHandler) Close() error {
h.withBufferLock(func() {
h.swap()
h.buf = nil
})
close(h.ch)
<-h.shutdown
return nil
}
func (h *bufStreamHandler) withBufferLock(fn func()) {
h.mx.Lock()
fn()
h.mx.Unlock()
}
func (h *bufStreamHandler) swap() {
if h.buf == nil || h.buf.Len() == 0 {
return
}
old := h.buf
h.buf = h.pool.Get()
h.ch <- old
}
// StreamHandler writes log messages to an io.Writer with the given format.
func StreamHandler(w io.Writer, fmtr Formatter) Handler {
var mu sync.Mutex
h := func(msg string, lvl Level, ctx []interface{}) {
mu.Lock()
w.Write(fmtr.Format(msg, lvl, ctx))
mu.Unlock()
}
return HandlerFunc(h)
}
// FilterFunc represents a function that can filter messages.
type FilterFunc func(msg string, lvl Level, ctx []interface{}) bool
// FilterHandler returns a handler that only writes messages to the wrapped
// handler if the given function evaluates true.
func FilterHandler(fn FilterFunc, h Handler) Handler {
c := &closeHandler{
Handler: HandlerFunc(func(msg string, lvl Level, ctx []interface{}) {
if fn(msg, lvl, ctx) {
h.Log(msg, lvl, ctx)
}
}),
}
if ch, ok := h.(io.Closer); ok {
c.Closer = ch
}
return c
}
// LevelFilterHandler returns a handler that
func LevelFilterHandler(maxLvl Level, h Handler) Handler {
return FilterHandler(func(msg string, lvl Level, ctx []interface{}) bool {
return lvl <= maxLvl
}, h)
}
// DiscardHandler does nothing, discarding all log messages.
func DiscardHandler() Handler {
return HandlerFunc(func(msg string, lvl Level, ctx []interface{}) {})
}
// closeHandler wraps a handler allowing it to close if it has a Close method.
type closeHandler struct {
io.Closer
Handler
}
func (h *closeHandler) Close() error {
if h.Closer != nil {
return h.Closer.Close()
}
return nil
}