-
Notifications
You must be signed in to change notification settings - Fork 12
/
output.go
391 lines (353 loc) · 8.95 KB
/
output.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package xlog
import (
"bytes"
"encoding/json"
"errors"
"io"
"os"
"sort"
"strings"
"sync"
"time"
"github.com/rs/xid"
"github.com/rs/xlog/internal/term"
)
// Output sends a log message fields to a destination.
type Output interface {
Write(fields map[string]interface{}) error
}
// OutputFunc is an adapter to allow the use of ordinary functions as Output handlers.
// If it is a function with the appropriate signature, OutputFunc(f) is a Output object
// that calls f on Write().
type OutputFunc func(fields map[string]interface{}) error
func (of OutputFunc) Write(fields map[string]interface{}) error {
return of(fields)
}
// OutputChannel is a send buffered channel between xlog and an Output.
type OutputChannel struct {
input chan map[string]interface{}
output Output
stop chan struct{}
}
// ErrBufferFull is returned when the output channel buffer is full and messages
// are discarded.
var ErrBufferFull = errors.New("buffer full")
// NewOutputChannel creates a consumer buffered channel for the given output
// with a default buffer of 100 messages.
func NewOutputChannel(o Output) *OutputChannel {
return NewOutputChannelBuffer(o, 100)
}
// NewOutputChannelBuffer creates a consumer buffered channel for the given output
// with a customizable buffer size.
func NewOutputChannelBuffer(o Output, bufSize int) *OutputChannel {
oc := &OutputChannel{
input: make(chan map[string]interface{}, bufSize),
output: o,
stop: make(chan struct{}),
}
go func() {
for {
select {
case msg := <-oc.input:
if err := o.Write(msg); err != nil {
critialLogger.Print("cannot write log message: ", err.Error())
}
case <-oc.stop:
close(oc.stop)
return
}
}
}()
return oc
}
// Write implements the Output interface
func (oc *OutputChannel) Write(fields map[string]interface{}) (err error) {
select {
case oc.input <- fields:
// Sent with success
default:
// Channel is full, message dropped
err = ErrBufferFull
}
return err
}
// Flush flushes all the buffered message to the output
func (oc *OutputChannel) Flush() {
for {
select {
case msg := <-oc.input:
if err := oc.output.Write(msg); err != nil {
critialLogger.Print("cannot write log message: ", err.Error())
}
default:
return
}
}
}
// Close closes the output channel and release the consumer's go routine.
func (oc *OutputChannel) Close() {
if oc.stop == nil {
return
}
oc.stop <- struct{}{}
<-oc.stop
oc.stop = nil
oc.Flush()
}
// Discard is an Output that discards all log message going thru it.
var Discard = OutputFunc(func(fields map[string]interface{}) error {
return nil
})
var bufPool = &sync.Pool{
New: func() interface{} {
return &bytes.Buffer{}
},
}
// MultiOutput routes the same message to serveral outputs.
// If one or more outputs return an error, the last error is returned.
type MultiOutput []Output
func (m MultiOutput) Write(fields map[string]interface{}) (err error) {
for _, o := range m {
e := o.Write(fields)
if e != nil {
err = e
}
}
return
}
// FilterOutput test a condition on the message and forward it to the child output
// if it returns true.
type FilterOutput struct {
Cond func(fields map[string]interface{}) bool
Output Output
}
func (f FilterOutput) Write(fields map[string]interface{}) (err error) {
if f.Output == nil {
return
}
if f.Cond(fields) {
return f.Output.Write(fields)
}
return
}
// LevelOutput routes messages to different output based on the message's level.
type LevelOutput struct {
Debug Output
Info Output
Warn Output
Error Output
Fatal Output
}
func (l LevelOutput) Write(fields map[string]interface{}) error {
var o Output
switch fields[KeyLevel] {
case "debug":
o = l.Debug
case "info":
o = l.Info
case "warn":
o = l.Warn
case "error":
o = l.Error
case "fatal":
o = l.Fatal
}
if o != nil {
return o.Write(fields)
}
return nil
}
// RecorderOutput stores the raw messages in it's Messages field. This output is useful for testing.
type RecorderOutput struct {
Messages []F
}
func (l *RecorderOutput) Write(fields map[string]interface{}) error {
if l.Messages == nil {
l.Messages = []F{fields}
} else {
l.Messages = append(l.Messages, fields)
}
return nil
}
// Reset empty the output from stored messages
func (l *RecorderOutput) Reset() {
l.Messages = []F{}
}
type consoleOutput struct {
w io.Writer
}
var isTerminal = term.IsTerminal
// NewConsoleOutput returns a Output printing message in a colored human readable form on the
// stderr. If the stderr is not on a terminal, a LogfmtOutput is returned instead.
func NewConsoleOutput() Output {
return NewConsoleOutputW(os.Stderr, NewLogfmtOutput(os.Stderr))
}
// NewConsoleOutputW returns a Output printing message in a colored human readable form with
// the provided writer. If the writer is not on a terminal, the noTerm output is returned.
func NewConsoleOutputW(w io.Writer, noTerm Output) Output {
if isTerminal(w) {
return consoleOutput{w: w}
}
return noTerm
}
func (o consoleOutput) Write(fields map[string]interface{}) error {
buf := bufPool.Get().(*bytes.Buffer)
defer func() {
buf.Reset()
bufPool.Put(buf)
}()
if ts, ok := fields[KeyTime].(time.Time); ok {
buf.Write([]byte(ts.Format("2006/01/02 15:04:05 ")))
}
if lvl, ok := fields[KeyLevel].(string); ok {
levelColor := blue
switch lvl {
case "debug":
levelColor = gray
case "warn":
levelColor = yellow
case "error":
levelColor = red
}
colorPrint(buf, strings.ToUpper(lvl[0:4]), levelColor)
buf.WriteByte(' ')
}
if msg, ok := fields[KeyMessage].(string); ok {
msg = strings.Replace(msg, "\n", "\\n", -1)
buf.Write([]byte(msg))
}
// Gather field keys
keys := []string{}
for k := range fields {
switch k {
case KeyLevel, KeyMessage, KeyTime:
continue
}
keys = append(keys, k)
}
// Sort fields by key names
sort.Strings(keys)
// Print fields using logfmt format
for _, k := range keys {
buf.WriteByte(' ')
colorPrint(buf, k, green)
buf.WriteByte('=')
if err := writeValue(buf, fields[k]); err != nil {
return err
}
}
buf.WriteByte('\n')
_, err := o.w.Write(buf.Bytes())
return err
}
type logfmtOutput struct {
w io.Writer
}
// NewLogfmtOutput returns a new output using logstash JSON schema v1
func NewLogfmtOutput(w io.Writer) Output {
return logfmtOutput{w: w}
}
func (o logfmtOutput) Write(fields map[string]interface{}) error {
buf := bufPool.Get().(*bytes.Buffer)
defer func() {
buf.Reset()
bufPool.Put(buf)
}()
// Gather field keys
keys := []string{}
for k := range fields {
switch k {
case KeyLevel, KeyMessage, KeyTime:
continue
}
keys = append(keys, k)
}
// Sort fields by key names
sort.Strings(keys)
// Prepend default fields in a specific order
keys = append([]string{KeyLevel, KeyMessage, KeyTime}, keys...)
l := len(keys)
for i, k := range keys {
buf.Write([]byte(k))
buf.WriteByte('=')
if err := writeValue(buf, fields[k]); err != nil {
return err
}
if i+1 < l {
buf.WriteByte(' ')
} else {
buf.WriteByte('\n')
}
}
_, err := o.w.Write(buf.Bytes())
return err
}
// NewJSONOutput returns a new JSON output with the given writer.
func NewJSONOutput(w io.Writer) Output {
enc := json.NewEncoder(w)
return OutputFunc(func(fields map[string]interface{}) error {
return enc.Encode(fields)
})
}
// NewLogstashOutput returns an output to generate logstash friendly JSON format.
func NewLogstashOutput(w io.Writer) Output {
return OutputFunc(func(fields map[string]interface{}) error {
lsf := map[string]interface{}{
"@version": 1,
}
for k, v := range fields {
switch k {
case KeyTime:
k = "@timestamp"
case KeyLevel:
if s, ok := v.(string); ok {
v = strings.ToUpper(s)
}
}
if t, ok := v.(time.Time); ok {
lsf[k] = t.Format(time.RFC3339)
} else {
lsf[k] = v
}
}
b, err := json.Marshal(lsf)
if err != nil {
return err
}
_, err = w.Write(b)
return err
})
}
// NewUIDOutput returns an output filter adding a globally unique id (using github.com/rs/xid)
// to all message going thru this output. The o parameter defines the next output to pass data
// to.
func NewUIDOutput(field string, o Output) Output {
return OutputFunc(func(fields map[string]interface{}) error {
fields[field] = xid.New().String()
return o.Write(fields)
})
}
// NewTrimOutput trims any field of type string with a value length greater than maxLen
// to maxLen.
func NewTrimOutput(maxLen int, o Output) Output {
return OutputFunc(func(fields map[string]interface{}) error {
for k, v := range fields {
if s, ok := v.(string); ok && len(s) > maxLen {
fields[k] = s[:maxLen]
}
}
return o.Write(fields)
})
}
// NewTrimFieldsOutput trims listed field fields of type string with a value length greater than maxLen
// to maxLen.
func NewTrimFieldsOutput(trimFields []string, maxLen int, o Output) Output {
return OutputFunc(func(fields map[string]interface{}) error {
for _, f := range trimFields {
if s, ok := fields[f].(string); ok && len(s) > maxLen {
fields[f] = s[:maxLen]
}
}
return o.Write(fields)
})
}