-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandler.go
65 lines (59 loc) · 1.66 KB
/
handler.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
package golog
import (
"bytes"
"io"
)
// A Handler is a leveled log handler with a formatter and several writers.
type Handler struct {
writers []io.WriteCloser
formatter *Formatter
level Level
isInternal bool
}
// NewHandler creates a new Handler of the given level with the formatter.
// Records with the lower level than the handler will be ignored.
func NewHandler(level Level, formatter *Formatter) *Handler {
return &Handler{
level: level,
formatter: formatter,
}
}
// AddWriter adds a writer to the Handler.
// The Write() method of the writer should be thread-safe.
func (h *Handler) AddWriter(w io.WriteCloser) {
h.writers = append(h.writers, w)
}
// Handle formats a record using its formatter, then writes the formatted result to all of its writers.
// Returns true if it can handle the record.
// The errors during writing will be logged by the internalLogger.
// It's not thread-safe, concurrent record may be written in a random order through different writers.
// But two records won't be mixed in a single line.
func (h *Handler) Handle(r *Record) bool {
if r.level >= h.level {
buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()
h.formatter.Format(r, buf)
content := buf.Bytes()
for _, w := range h.writers {
_, err := w.Write(content)
if err != nil && !h.isInternal {
logError(err)
}
}
bufPool.Put(buf)
return true
}
return false
}
// Close closes all its writers.
// It's safe to call this method more than once,
// but it's unsafe to call its writers' Close() more than once.
func (h *Handler) Close() {
for _, w := range h.writers {
err := w.Close()
if err != nil {
logError(err)
}
}
h.writers = nil
}