-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreflect.go
210 lines (188 loc) · 5.21 KB
/
reflect.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
package rfc5424
import (
"log"
"os"
"path"
"reflect"
"regexp"
"strconv"
"strings"
)
const (
defaultSeverity = Info
defaultFacility = Local0
defaultStructuredDataID = "0@local"
)
var (
defaultHostname = func() string {
h, err := os.Hostname()
if err != nil {
panic(err)
}
return h
}()
defaultAppName = func() string {
return path.Base(os.Args[0])
}()
defaultProcessID = func() string {
return strconv.FormatInt(int64(os.Getpid()), 10)
}()
)
type reflection struct {
Type reflect.Type
SeverityFieldIndex int
SeverityDefault Severity
FacilityFieldIndex int
FacilityDefault Facility
TimestampFieldIndex int
HostnameFieldIndex int
AppNameFieldIndex int
AppNameDefault string
ProcessIDFieldIndex int
MessageIDFieldIndex int
MessageIDDefault string
MessageFieldIndex int
SDIDDefault string
StructuredDataFieldReflections []structuredDataFieldReflection
}
type structuredDataFieldReflection struct {
FieldIndex int
OmitEmpty bool
FieldName string
SdID string
}
func (r *reflection) GetStructuredDataFieldReflection(
SdID string, FieldName string) *structuredDataFieldReflection {
for _, fieldReflection := range r.StructuredDataFieldReflections {
if fieldReflection.SdID == SdID && fieldReflection.FieldName == FieldName {
return &fieldReflection
}
}
return nil
}
var reflectionCache = map[string][]*reflection{}
func Reflect(t reflect.Type) *reflection {
reflectionList, ok := reflectionCache[t.Name()]
if !ok {
r := reflectImpl(t)
reflectionCache[t.Name()] = []*reflection{r}
return r
}
for _, r := range reflectionList {
if r.Type == t {
return r
}
}
r := reflectImpl(t)
reflectionCache[t.Name()] = append(reflectionList, r)
return r
}
var sdRegexp = regexp.MustCompile("^(\\d+@\\S+)( (.*))?$")
func reflectImpl(t reflect.Type) *reflection {
r := reflection{
Type: t,
SeverityFieldIndex: -1,
SeverityDefault: defaultSeverity,
FacilityFieldIndex: -1,
FacilityDefault: defaultFacility,
TimestampFieldIndex: -1,
HostnameFieldIndex: -1,
AppNameFieldIndex: -1,
AppNameDefault: defaultAppName,
ProcessIDFieldIndex: -1,
MessageIDFieldIndex: -1,
MessageIDDefault: t.Name(),
MessageFieldIndex: -1,
StructuredDataFieldReflections: []structuredDataFieldReflection{},
}
for fieldIndex := 0; fieldIndex < t.NumField(); fieldIndex++ {
field := t.Field(fieldIndex)
fieldTag := field.Tag.Get("log")
switch field.Name {
case "Severity":
r.SeverityFieldIndex = fieldIndex
if fieldTag != "" {
severity, ok := severityNames[fieldTag]
if !ok {
panic("invalid tag on Severity field")
}
r.SeverityDefault = severity
}
case "Facility":
r.FacilityFieldIndex = fieldIndex
if fieldTag != "" {
facility, ok := facilityNames[fieldTag]
if !ok {
panic("invalid tag on Facility field")
}
r.FacilityDefault = facility
}
case "Timestamp":
r.TimestampFieldIndex = fieldIndex
case "Hostname":
r.HostnameFieldIndex = fieldIndex
case "AppName":
r.AppNameFieldIndex = fieldIndex
if fieldTag != "" {
r.AppNameDefault = fieldTag
}
case "ProcessID":
r.ProcessIDFieldIndex = fieldIndex
case "MessageID":
r.MessageIDFieldIndex = fieldIndex
if fieldTag != "" {
r.MessageIDDefault = fieldTag
}
case "SDID":
if fieldTag != "" {
r.SDIDDefault = fieldTag
}
case "Message":
r.MessageFieldIndex = fieldIndex
default:
// Message or Structured Data fields
// if the field is private and not tagged, ignore it
if fieldTag == "" && field.PkgPath != "" {
// Field is not exported, skip it
continue
}
tagParts := strings.Split(fieldTag, ",")
// If the field is marked message it contains the message
if len(tagParts) > 1 && tagParts[0] == "" && tagParts[1] == "message" {
r.MessageFieldIndex = fieldIndex
continue
}
fieldReflection := structuredDataFieldReflection{}
fieldReflection.FieldIndex = fieldIndex
fieldReflection.FieldName = tagParts[0]
if r.SDIDDefault != "" {
fieldReflection.SdID = r.SDIDDefault
} else {
fieldReflection.SdID = defaultStructuredDataID
}
matches := sdRegexp.FindAllStringSubmatch(fieldReflection.FieldName, -1)
if matches != nil {
fieldReflection.SdID = matches[0][1]
fieldReflection.FieldName = matches[0][3]
}
if fieldReflection.FieldName == "" {
// Generate a field name by converting the first letter to lowercase
fieldReflection.FieldName = strings.ToLower(field.Name[0:1]) + field.Name[1:]
}
if len(tagParts) > 1 {
for _, tagAttr := range tagParts[1:] {
switch tagParts[1] {
case "omitempty":
fieldReflection.OmitEmpty = true
default:
log.Panicf("unknown tag %s on field %s of %s",
tagAttr, field.Name, t.Name())
}
}
}
r.StructuredDataFieldReflections = append(r.StructuredDataFieldReflections,
fieldReflection)
}
}
return &r
}