-
Notifications
You must be signed in to change notification settings - Fork 22
/
finders.go
58 lines (53 loc) · 1.35 KB
/
finders.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
package jl
import (
"encoding/json"
"strings"
)
// FieldFinder locates a field in the Entry and returns it.
type FieldFinder func(entry *Entry) interface{}
// ByNames locates fields by their top-level key name in the JSON log entry, and returns the field as a json.RawMessage.
func ByNames(names ...string) FieldFinder {
return func(entry *Entry) interface{} {
for _, name := range names {
if v, ok := getDeep(entry, name); ok {
return v
}
}
return nil
}
}
func getDeep(entry *Entry, name string) (interface{}, bool) {
parts := strings.SplitN(name, ".", 2)
key := parts[0]
v, ok := entry.Partials[key]
if !ok {
return nil, false
}
if len(parts) == 1 {
return v, true
}
var partials map[string]json.RawMessage
err := json.Unmarshal(v, &partials)
if err != nil {
return nil, false
}
return getDeep(&Entry{
Partials: partials,
Raw: v,
}, parts[1])
}
// LogrusErrorFinder finds logrus error in the JSON log and returns it as a LogrusError.
func LogrusErrorFinder(entry *Entry) interface{} {
var errStr, stack string
if errV, ok := entry.Partials["error"]; !ok {
return nil
} else if err := json.Unmarshal(errV, &errStr); err != nil {
return nil
}
if stackV, ok := entry.Partials["stack"]; !ok {
return nil
} else if err := json.Unmarshal(stackV, &stack); err != nil {
return nil
}
return LogrusError{errStr, stack}
}