-
Notifications
You must be signed in to change notification settings - Fork 50
/
fields.go
107 lines (86 loc) · 2.21 KB
/
fields.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
package log
import (
"fmt"
"reflect"
"time"
)
func mergeFields(receiver map[string]interface{}, input map[string]interface{}) map[string]interface{} {
newMap := make(map[string]interface{}, len(receiver)+len(input))
for k, v := range receiver {
if k == "" {
continue
}
newMap[k] = prepareForLog(v)
}
for k, v := range input {
if k == "" {
continue
}
newMap[k] = prepareForLog(v)
}
return newMap
}
func prepareForLog(v interface{}) interface{} {
switch t := v.(type) {
case error:
// Otherwise errors are ignored by `encoding/json`
return t.Error()
case time.Time:
return v
case map[string]interface{}:
// perform a deep copy of any maps contained in this map element to ensure we own the object completely
// also makes sure, that all nested values get prepared to be written as well
return mergeFields(t, nil)
default:
// same as before, but handle the case of the map mapping to something
// different than interface{}
// should quite rarely get hit, otherwise you are using too complex objects for your logs
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Map:
iter := rv.MapRange()
newMap := make(map[string]interface{}, rv.Len())
for iter.Next() {
keyValue := iter.Key()
key := fmt.Sprint(keyValue.Interface())
if key == "" {
continue
}
elemValue := iter.Value()
newMap[key] = prepareForLog(elemValue.Interface())
}
return newMap
case reflect.Ptr, reflect.Interface:
if rv.IsNil() {
return nil
}
return prepareForLog(rv.Elem().Interface())
case reflect.Struct:
rvt := rv.Type()
newMap := make(map[string]interface{}, rv.NumField())
for i := 0; i < rv.NumField(); i++ {
field := rv.Field(i)
if !field.CanInterface() {
continue
}
fieldName := rvt.Field(i).Name
if fieldName == "" {
continue
}
newMap[fieldName] = prepareForLog(field.Interface())
}
return newMap
case reflect.Slice, reflect.Array:
if rv.Kind() == reflect.Slice && rv.IsNil() {
return nil
}
newArray := make([]interface{}, rv.Len())
for i := range newArray {
newArray[i] = prepareForLog(rv.Index(i).Interface())
}
return newArray
default:
return v
}
}
}