forked from humanlogio/humanlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_handler.go
261 lines (227 loc) · 5.43 KB
/
json_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
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
package humanlog
import (
"bytes"
"encoding/json"
"fmt"
"math"
"sort"
"strings"
"text/tabwriter"
"time"
"github.com/fatih/color"
)
// JSONHandler can handle logs emmited by logrus.TextFormatter loggers.
type JSONHandler struct {
buf *bytes.Buffer
out *tabwriter.Writer
truncKV int
Opts *HandlerOptions
Level string
Time time.Time
Message string
Fields map[string]string
last map[string]string
}
func checkEachUntilFound(fieldList []string, found func(string) bool) bool {
for _, field := range fieldList {
if found(field) {
return true
}
}
return false
}
// supportedTimeFields enumerates supported timestamp field names
var supportedTimeFields = []string{"time", "ts", "@timestamp", "timestamp"}
// supportedMessageFields enumarates supported Message field names
var supportedMessageFields = []string{"message", "msg"}
// supportedLevelFields enumarates supported level field names
var supportedLevelFields = []string{"level", "lvl", "loglevel", "severity"}
func (h *JSONHandler) clear() {
h.Level = ""
h.Time = time.Time{}
h.Message = ""
h.last = h.Fields
h.Fields = make(map[string]string)
if h.buf != nil {
h.buf.Reset()
}
}
// TryHandle tells if this line was handled by this handler.
func (h *JSONHandler) TryHandle(d []byte) bool {
if !h.UnmarshalJSON(d) {
h.clear()
return false
}
return true
}
// UnmarshalJSON sets the fields of the handler.
func (h *JSONHandler) UnmarshalJSON(data []byte) bool {
raw := make(map[string]interface{})
err := json.Unmarshal(data, &raw)
if err != nil {
return false
}
checkEachUntilFound(supportedTimeFields, func(field string) bool {
time, ok := tryParseTime(raw[field])
if ok {
h.Time = time
delete(raw, field)
}
return ok
})
checkEachUntilFound(supportedMessageFields, func(field string) bool {
msg, ok := raw[field].(string)
if ok {
h.Message = msg
delete(raw, field)
}
return ok
})
checkEachUntilFound(supportedLevelFields, func(field string) bool {
lvl, ok := raw[field]
if !ok {
return false
}
if strLvl, ok := lvl.(string); ok {
h.Level = strLvl
} else if flLvl, ok := lvl.(float64); ok {
h.Level = convertBunyanLogLevel(flLvl)
} else {
h.Level = "???"
}
delete(raw, field)
return true
})
if h.Fields == nil {
h.Fields = make(map[string]string)
}
for key, val := range raw {
switch v := val.(type) {
case float64:
if v-math.Floor(v) < 0.000001 && v < 1e9 {
// looks like an integer that's not too large
h.Fields[key] = fmt.Sprintf("%d", int(v))
} else {
h.Fields[key] = fmt.Sprintf("%g", v)
}
case string:
h.Fields[key] = fmt.Sprintf("%q", v)
default:
h.Fields[key] = fmt.Sprintf("%v", v)
}
}
return true
}
func (h *JSONHandler) setField(key, val []byte) {
if h.Fields == nil {
h.Fields = make(map[string]string)
}
h.Fields[string(key)] = string(val)
}
// Prettify the output in a logrus like fashion.
func (h *JSONHandler) Prettify(skipUnchanged bool) []byte {
defer h.clear()
if h.out == nil {
if h.Opts == nil {
h.Opts = DefaultOptions
}
h.buf = bytes.NewBuffer(nil)
h.out = tabwriter.NewWriter(h.buf, 0, 1, 0, '\t', 0)
}
var (
msgColor *color.Color
msgAbsentColor *color.Color
)
if h.Opts.LightBg {
msgColor = h.Opts.MsgLightBgColor
msgAbsentColor = h.Opts.MsgAbsentLightBgColor
} else {
msgColor = h.Opts.MsgDarkBgColor
msgAbsentColor = h.Opts.MsgAbsentDarkBgColor
}
msgColor = color.New(color.FgHiWhite)
msgAbsentColor = color.New(color.FgHiWhite)
var msg string
if h.Message == "" {
msg = msgAbsentColor.Sprint("<no msg>")
} else {
msg = msgColor.Sprint(h.Message)
}
lvl := strings.ToUpper(h.Level)[:imin(4, len(h.Level))]
var level string
switch h.Level {
case "debug":
level = h.Opts.DebugLevelColor.Sprint(lvl)
case "info":
level = h.Opts.InfoLevelColor.Sprint(lvl)
case "warn", "warning":
level = h.Opts.WarnLevelColor.Sprint(lvl)
case "error":
level = h.Opts.ErrorLevelColor.Sprint(lvl)
case "fatal", "panic":
level = h.Opts.FatalLevelColor.Sprint(lvl)
default:
level = h.Opts.UnknownLevelColor.Sprint(lvl)
}
var timeColor *color.Color
if h.Opts.LightBg {
timeColor = h.Opts.TimeLightBgColor
} else {
timeColor = h.Opts.TimeDarkBgColor
}
_, _ = fmt.Fprintf(h.out, "%s |%s| %s\t %s",
timeColor.Sprint(h.Time.Format(h.Opts.TimeFormat)),
level,
msg,
strings.Join(h.joinKVs(skipUnchanged, "="), "\t "),
)
_ = h.out.Flush()
return h.buf.Bytes()
}
func (h *JSONHandler) joinKVs(skipUnchanged bool, sep string) []string {
kv := make([]string, 0, len(h.Fields))
for k, v := range h.Fields {
if !h.Opts.shouldShowKey(k) {
continue
}
if skipUnchanged {
if lastV, ok := h.last[k]; ok && lastV == v && !h.Opts.shouldShowUnchanged(k) {
continue
}
}
kstr := h.Opts.KeyColor.Sprint(k)
var vstr string
if h.Opts.Truncates && len(v) > h.Opts.TruncateLength {
vstr = v[:h.Opts.TruncateLength] + "..."
} else {
vstr = v
}
vstr = h.Opts.ValColor.Sprint(vstr)
kv = append(kv, kstr+sep+vstr)
}
sort.Strings(kv)
if h.Opts.SortLongest {
sort.Stable(byLongest(kv))
}
return kv
}
// convertBunyanLogLevel returns a human readable log level given a numerical bunyan level
// https://github.com/trentm/node-bunyan#levels
func convertBunyanLogLevel(level float64) string {
switch level {
case 10:
return "trace"
case 20:
return "debug"
case 30:
return "info"
case 40:
return "warn"
case 50:
return "error"
case 60:
return "fatal"
default:
return "???"
}
}