-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathconfig_json.go
145 lines (126 loc) · 3.27 KB
/
config_json.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
package timber
import (
"encoding/json"
"fmt"
"log"
"os"
"reflect"
)
// Granulars are overriding levels that can be either
// package paths or package path + function name
type JSONGranular struct {
Level string `xml:"level"`
Path string `xml:"path"`
}
type JSONProperty struct {
Name string `xml:"name"`
Value string `xml:"value"`
}
type JSONFilter struct {
Enabled bool
Tag string
Type string
Level string
Format JSONProperty
Properties []JSONProperty
Granulars []JSONGranular
}
type JSONConfig struct {
Filters []JSONFilter
}
// Loads the configuration from an JSON file (as you were probably expecting)
func (t *Timber) LoadJSONConfig(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 json config file: %s %v", filename, err)
}
defer file.Close()
config := JSONConfig{}
err = json.NewDecoder(file).Decode(&config)
if err != nil {
return fmt.Errorf("TIMBER! Can't parse json config file: %s %v", filename, err)
}
for _, filter := range config.Filters {
if !filter.Enabled {
continue
}
level := getLevel(filter.Level)
formatter := getJSONFormatter(filter)
if err != nil {
return err
}
granulars := make(map[string]Level)
for _, granular := range filter.Granulars {
granulars[granular.Path] = getLevel(granular.Level)
}
configLogger := ConfigLogger{Level: level, Formatter: formatter, Granulars: granulars}
switch filter.Type {
case "console":
configLogger.LogWriter = new(ConsoleWriter)
case "socket":
configLogger.LogWriter, err = getJSONSocketWriter(filter)
if err != nil {
return err
}
case "file":
configLogger.LogWriter, err = getJSONFileWriter(filter)
if 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 getJSONFormatter(filter JSONFilter) LogFormatter {
format := ""
property := JSONProperty{}
// 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 getJSONSocketWriter(filter JSONFilter) (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 getJSONFileWriter(filter JSONFilter) (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)
}