-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
173 lines (143 loc) · 3.75 KB
/
writer.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
package kvwriter
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"sort"
"strconv"
"sync"
"github.com/jeremywohl/flatten"
)
var (
kvBufPool = sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(make([]byte, 0, 100))
},
}
)
// Formatter transforms the input into a formatted string.
type Formatter func(interface{}) string
// KeyValueWriter parses the JSON input and writes it in a human-friendly format to Out.
type KeyValueWriter struct {
// Out is the output destination.
Out io.Writer
// PairsDelimiter defines a character to delimit individual pairs. (default: ' ')
PairsDelimiter rune
// KeyValueDelimiter defines a character to delimit key and value. (default: '=')
KeyValueDelimiter rune
// QuoteValues defines if you want to quote values. If enabled it will quote all values
// for consistency. If PairsDelimiter doesn't occur in the keys nor values
// then you don't need to quote values. (default: true)
QuoteValues bool
// KeysExclude defines keys to not display in output. JSON structure is flattened so
// json '{"event": {"name": "x"}}' would produce 'event.name' key with 'x' as a value.
KeysExclude []string
FormatKey Formatter
FormatValue Formatter
FormatExtra func(map[string]interface{}, *bytes.Buffer) error
}
// NewKeyValueWriter creates and initializes a new KeyValueWriter.
func NewKeyValueWriter(options ...func(w *KeyValueWriter)) KeyValueWriter {
w := KeyValueWriter{
Out: os.Stdout,
PairsDelimiter: ' ',
KeyValueDelimiter: '=',
QuoteValues: true,
}
for _, opt := range options {
opt(&w)
}
return w
}
// Write transforms the JSON input with formatters and appends to w.Out.
func (w KeyValueWriter) Write(p []byte) (n int, err error) {
var buf = kvBufPool.Get().(*bytes.Buffer)
defer func() {
buf.Reset()
kvBufPool.Put(buf)
}()
var evt map[string]interface{}
d := json.NewDecoder(bytes.NewReader(p))
d.UseNumber()
err = d.Decode(&evt)
if err != nil {
return n, fmt.Errorf("cannot decode event: %s", err)
}
evt, err = flatten.Flatten(evt, "", flatten.DotStyle)
if err != nil {
return n, fmt.Errorf("cannot flatten event: %s", err)
}
w.writePairs(evt, buf)
if w.FormatExtra != nil {
err = w.FormatExtra(evt, buf)
if err != nil {
return n, err
}
}
err = buf.WriteByte('\n')
if err != nil {
return n, err
}
_, err = buf.WriteTo(w.Out)
return len(p), err
}
// writePairs appends formatted key-value pairs to buf.
func (w KeyValueWriter) writePairs(evt map[string]interface{}, buf *bytes.Buffer) {
var keys = make([]string, 0, len(evt))
for key := range evt {
var isExcluded bool
for _, excluded := range w.KeysExclude {
if key == excluded {
isExcluded = true
break
}
}
if isExcluded {
continue
}
keys = append(keys, key)
}
sort.Strings(keys)
fk := defaultFormatKey
fv := defaultFormatValue
if w.FormatKey != nil {
fk = w.FormatKey
}
if w.FormatValue != nil {
fv = w.FormatValue
}
for i, key := range keys {
buf.WriteString(fk(key))
buf.WriteRune(w.KeyValueDelimiter)
switch value := evt[key].(type) {
case string:
buf.WriteString(quoteValue(fv(value), w.QuoteValues))
case json.Number:
buf.WriteString(quoteValue(fv(value), w.QuoteValues))
default:
b, err := json.Marshal(value)
if err != nil {
buf.WriteString(quoteValue(fmt.Sprintf("[error: %v]", err), w.QuoteValues))
} else {
buf.WriteString(quoteValue(fv(b), w.QuoteValues))
}
}
if i < len(keys)-1 { // Skip PairsDelimiter for last field
buf.WriteRune(w.PairsDelimiter)
}
}
}
func quoteValue(v string, q bool) string {
if q {
return strconv.Quote(v)
}
return v
}
func defaultFormatKey(i interface{}) string {
return fmt.Sprintf("%s", i)
}
func defaultFormatValue(i interface{}) string {
return fmt.Sprintf("%s", i)
}