/
handler.go
108 lines (90 loc) · 2.51 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
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
package text
import (
"fmt"
"io"
"sync"
"time"
"github.com/segmentio/events"
)
// DefaultTimeFormat is the default time format set on Handler.
const DefaultTimeFormat = "2006-01-02 15:04:05.000"
// Handler is an event handler which format events in a human-readable format
// and writes them to its output.
//
// It is safe to use a handler concurrently from multiple goroutines.
type Handler struct {
Output io.Writer // writer receiving the formatted events
Prefix string // written at the beginning of each formatted event
TimeFormat string // format used for the event's time
TimeLocation *time.Location // location to output the event time in
EnableArgs bool // output detailes of each args in the events
// synchronizes writes to the output
mutex sync.Mutex
}
// NewHandler creates a new handler which writes to output with a prefix on each
// line.
func NewHandler(prefix string, output io.Writer) *Handler {
return &Handler{
Output: output,
Prefix: prefix,
TimeFormat: DefaultTimeFormat,
}
}
// HandleEvent satisfies the events.Handler interface.
func (h *Handler) HandleEvent(e *events.Event) {
buf := bufferPool.Get().(*buffer)
buf.b = buf.b[:0]
buf.b = append(buf.b, h.Prefix...)
if fmt := h.TimeFormat; len(fmt) != 0 {
loc := h.TimeLocation
if loc == nil {
loc = time.Local
}
buf.b = e.Time.In(loc).AppendFormat(buf.b, fmt)
buf.b = append(buf.b, " - "...)
}
if len(e.Source) != 0 {
buf.b = append(buf.b, e.Source...)
buf.b = append(buf.b, " - "...)
}
buf.b = append(buf.b, e.Message...)
buf.b = append(buf.b, '\n')
if h.EnableArgs {
hasError := false
for _, a := range e.Args {
if _, ok := a.Value.(error); ok {
hasError = true
} else {
buf.b = append(buf.b, '\t')
buf.b = append(buf.b, a.Name...)
buf.b = append(buf.b, ':', ' ')
fmt.Fprintf(buf, "%v\n", a.Value)
}
}
if hasError {
fmt.Fprint(buf, "\terrors:\n")
for _, a := range e.Args {
if err, ok := a.Value.(error); ok {
fmt.Fprintf(buf, "\t\t- %+v\n", err)
}
}
}
}
h.mutex.Lock()
h.Output.Write(buf.b)
h.mutex.Unlock()
bufferPool.Put(buf)
}
// This buffer type is used as an optimization, it's faster than the standard
// bytes.Buffer because it doesn't expose such a rich API.
type buffer struct {
b []byte
}
func (buf *buffer) Write(b []byte) (n int, err error) {
buf.b = append(buf.b, b...)
n = len(b)
return
}
var bufferPool = sync.Pool{
New: func() interface{} { return &buffer{make([]byte, 0, 4096)} },
}