-
Notifications
You must be signed in to change notification settings - Fork 11
/
config_xml.go
143 lines (125 loc) · 3.53 KB
/
config_xml.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
package timber
import (
"encoding/xml"
"fmt"
"log"
"os"
"reflect"
)
// Granulars are overriding levels that can be either
// package paths or package path + function name
type XMLGranular struct {
Level string `xml:"level"`
Path string `xml:"path"`
}
// match the log4go structure so i don't have to change my configs
type XMLProperty struct {
Name string `xml:"name,attr"`
Value string `xml:",chardata"`
}
type XMLFilter struct {
XMLName xml.Name `xml:"filter"`
Enabled bool `xml:"enabled,attr"`
Tag string `xml:"tag"`
Type string `xml:"type"`
Level string `xml:"level"`
Format XMLProperty `xml:"format"`
Properties []XMLProperty `xml:"property"`
Granulars []XMLGranular `xml:"granular"`
}
type XMLConfig struct {
XMLName xml.Name `xml:"logging"`
Filters []XMLFilter `xml:"filter"`
}
// Loads the configuration from an XML file (as you were probably expecting)
func (t *Timber) LoadXMLConfig(filename string) error {
if len(filename) <= 0 {
return fmt.Errorf("Empty filename")
}
file, err := os.Open(filename)
if err != nil {
return fmt.Errorf("TIMBER! Can't load xml config file: %s %v", filename, err)
}
defer file.Close()
config := XMLConfig{}
err = xml.NewDecoder(file).Decode(&config)
if err != nil {
return fmt.Errorf("TIMBER! Can't parse xml config file: %s %v", filename, err)
}
for _, filter := range config.Filters {
if !filter.Enabled {
continue
}
level := getLevel(filter.Level)
formatter := getXMLFormatter(filter)
granulars := make(map[string]Level)
for _, granular := range filter.Granulars {
granulars[granular.Path] = getLevel(granular.Level)
}
configLogger := ConfigLogger{Level: level, Formatter: formatter, Granulars: granulars}
var err error
switch filter.Type {
case "console":
configLogger.LogWriter = new(ConsoleWriter)
case "socket":
if configLogger.LogWriter, err = getXMLSocketWriter(filter); err != nil {
return err
}
case "file":
if configLogger.LogWriter, err = getXMLFileWriter(filter); err != nil {
return err
}
default:
log.Printf("TIMBER! Warning unrecognized filter in config file: %v\n", filter.Tag)
continue
}
t.AddLogger(configLogger)
}
return nil
}
func getXMLFormatter(filter XMLFilter) LogFormatter {
format := ""
property := XMLProperty{}
// If format field is set then use it's value, otherwise
// attempt to get the format field from the filters properties
if !reflect.DeepEqual(filter.Format, property) {
format = filter.Format.Value
} else {
for _, prop := range filter.Properties {
if prop.Name == "format" {
format = prop.Value
}
}
}
// If empty format set the default as just the message
if format == "" {
format = "%M"
}
return NewPatFormatter(format)
}
func getXMLSocketWriter(filter XMLFilter) (LogWriter, error) {
var protocol, endpoint string
for _, property := range filter.Properties {
if property.Name == "protocol" {
protocol = property.Value
} else if property.Name == "endpoint" {
endpoint = property.Value
}
}
if protocol == "" || endpoint == "" {
return nil, fmt.Errorf("TIMBER! Missing protocol or endpoint for socket log writer")
}
return NewSocketWriter(protocol, endpoint)
}
func getXMLFileWriter(filter XMLFilter) (LogWriter, error) {
filename := ""
for _, property := range filter.Properties {
if property.Name == "filename" {
filename = property.Value
}
}
if filename == "" {
return nil, fmt.Errorf("TIMBER! Missing filename for file log writer")
}
return NewFileWriter(filename)
}