forked from AliyunContainerService/log-pilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
61 lines (52 loc) · 1.74 KB
/
format.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
package pilot
import "fmt"
type FormatConverter func(info *LogInfoNode) (map[string]string, error)
var converters = make(map[string]FormatConverter)
func Register(format string, converter FormatConverter) {
converters[format] = converter
}
func Convert(info *LogInfoNode) (map[string]string, error) {
converter := converters[info.value]
if converter == nil {
return nil, fmt.Errorf("unsupported log format: %s", info.value)
}
return converter(info)
}
type SimpleConverter struct {
properties map[string]bool
}
func init() {
simpleConverter := func(properties []string) FormatConverter {
return func(info *LogInfoNode) (map[string]string, error) {
validProperties := make(map[string]bool)
for _, property := range properties {
validProperties[property] = true
}
ret := make(map[string]string)
for k, v := range info.children {
if _, ok := validProperties[k]; !ok {
return nil, fmt.Errorf("%s is not a valid properties for format %s", k, info.value)
}
ret[k] = v.value
}
return ret, nil
}
}
Register("nonex", simpleConverter([]string{}))
Register("csv", simpleConverter([]string{"time_key", "time_format", "keys"}))
Register("json", simpleConverter([]string{"time_key", "time_format"}))
Register("regexp", simpleConverter([]string{"time_key", "time_format"}))
Register("apache2", simpleConverter([]string{}))
Register("apache_error", simpleConverter([]string{}))
Register("nginx", simpleConverter([]string{}))
Register("regexp", func(info *LogInfoNode) (map[string]string, error) {
ret, err := simpleConverter([]string{"pattern", "time_format"})(info)
if err != nil {
return ret, err
}
if ret["pattern"] == "" {
return nil, fmt.Errorf("regex pattern can not be emtpy")
}
return ret, nil
})
}