-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconfig.go
More file actions
254 lines (224 loc) · 6.96 KB
/
Copy pathconfig.go
File metadata and controls
254 lines (224 loc) · 6.96 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package config
import (
"encoding/xml"
"errors"
"github.com/devfeel/dotlog/const"
"github.com/devfeel/dotlog/util/file"
"io/ioutil"
)
const (
ConfigType_Xml = "xml"
)
var (
GlobalAppConfig *AppConfig
)
func init() {
GlobalAppConfig = &AppConfig{}
}
type (
AppConfig struct {
XMLName xml.Name `xml:"config" json:"-"`
Global *GlobalConfig `xml:"global"`
Targets *TargetList `xml:"targets"`
Variables []*VariableConfig `xml:"variable>var"`
Loggers []*LoggerConfig `xml:"loggers>logger"`
}
GlobalConfig struct {
IsLog bool `xml:"islog,attr"`
ChanSize int `xml:"chansize,attr"` //日志队列长度,默认为DefaultChanSize = 1000
InnerLogPath string `xml:"innerlogpath,attr"`
InnerLogEncode string `xml:"innerlogencode,attr"`
}
VariableConfig struct {
Name string `xml:"name,attr"`
Value string `xml:"value,attr"`
}
LoggerConfig struct {
Name string `xml:"name,attr"`
IsLog bool `xml:"islog,attr"`
Layout string `xml:"layout,attr"`
ConfigMode string `xml:"configmode,attr"`
Levels []*LoggerLevelConfig `xml:"level"`
}
LoggerLevelConfig struct {
Level string `xml:"level,attr"`
Targets string `xml:"targets,attr"`
IsLog bool `xml:"islog,attr"`
}
TargetList struct {
FileTargets []*FileTargetConfig `xml:"file>target"`
UdpTargets []*UdpTargetConfig `xml:"udp>target"`
HttpTargets []*HttpTargetConfig `xml:"http>target"`
EMailTargets []*EMailTargetConfig `xml:"email>target"`
FmtTargets []*FmtTargetConfig `xml:"fmt>target"`
JSONTargets []*JSONTargetConfig `xml:"json>target"`
}
FileTargetConfig struct {
Name string `xml:"name,attr"`
IsLog bool `xml:"islog,attr"`
Layout string `xml:"layout,attr"`
Encode string `xml:"encode,attr"`
FileMaxSize int64 `xml:"filemaxsize,attr"` //日志文件最大容量,单位为KB
FileName string `xml:"filename,attr"`
MaxBackups int `xml:"maxbackups,attr"` //保留备份文件数量,0表示不清理
}
UdpTargetConfig struct {
Name string `xml:"name,attr"`
IsLog bool `xml:"islog,attr"`
Layout string `xml:"layout,attr"`
Encode string `xml:"encode,attr"`
RemoteIP string `xml:"remoteip,attr"`
}
HttpTargetConfig struct {
Name string `xml:"name,attr"`
IsLog bool `xml:"islog,attr"`
Layout string `xml:"layout,attr"`
Encode string `xml:"encode,attr"`
HttpUrl string `xml:"httpurl,attr"`
}
FmtTargetConfig struct {
Name string `xml:"name,attr"`
IsLog bool `xml:"islog,attr"`
Layout string `xml:"layout,attr"`
Encode string `xml:"encode,attr"`
}
EMailTargetConfig struct {
Name string `xml:"name,attr"`
IsLog bool `xml:"islog,attr"`
Layout string `xml:"layout,attr"`
Encode string `xml:"encode,attr"`
MailServer string `xml:"mailserver,attr"`
MailAccount string `xml:"mailaccount,attr"`
MailNickName string `xml:"mailnickname,attr"`
MailPassword string `xml:"mailpassword,attr"`
ToMail string `xml:"tomail,attr"`
Subject string `xml:"subject,attr"`
}
)
func (c *GlobalConfig) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type innerType GlobalConfig // new type to prevent recursion
item := innerType{
IsLog: true,
InnerLogEncode: _const.DefaultEncode,
InnerLogPath: _file.GetCurrentDirectory(),
}
if err := d.DecodeElement(&item, &start); err != nil {
return err
}
*c = (GlobalConfig)(item)
return nil
}
func (c *LoggerLevelConfig) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type innerType LoggerLevelConfig // new type to prevent recursion
item := innerType{
IsLog: true,
}
if err := d.DecodeElement(&item, &start); err != nil {
return err
}
*c = (LoggerLevelConfig)(item)
return nil
}
func (c *FileTargetConfig) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type innerType FileTargetConfig // new type to prevent recursion
item := innerType{
IsLog: true,
}
if err := d.DecodeElement(&item, &start); err != nil {
return err
}
*c = (FileTargetConfig)(item)
return nil
}
func (c *UdpTargetConfig) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type innerType UdpTargetConfig // new type to prevent recursion
item := innerType{
IsLog: true,
}
if err := d.DecodeElement(&item, &start); err != nil {
return err
}
*c = (UdpTargetConfig)(item)
return nil
}
func (c *HttpTargetConfig) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type innerType HttpTargetConfig // new type to prevent recursion
item := innerType{
IsLog: true,
}
if err := d.DecodeElement(&item, &start); err != nil {
return err
}
*c = (HttpTargetConfig)(item)
return nil
}
func (c *EMailTargetConfig) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type innerType EMailTargetConfig // new type to prevent recursion
item := innerType{
IsLog: true,
}
if err := d.DecodeElement(&item, &start); err != nil {
return err
}
*c = (EMailTargetConfig)(item)
return nil
}
func (c *FmtTargetConfig) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type innerType FmtTargetConfig // new type to prevent recursion
item := innerType{
IsLog: true,
}
if err := d.DecodeElement(&item, &start); err != nil {
return err
}
*c = (FmtTargetConfig)(item)
return nil
}
func (c *LoggerConfig) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type innerType LoggerConfig // new type to prevent recursion
item := innerType{
IsLog: true,
}
if err := d.DecodeElement(&item, &start); err != nil {
return err
}
*c = (LoggerConfig)(item)
return nil
}
//初始化配置文件
//如果发生异常,返回异常
func InitConfig(configFile string, confType ...interface{}) (config *AppConfig, err error) {
//检查配置文件有效性
//1、按绝对路径检查
//2、尝试在当前进程根目录下寻找
//3、尝试在当前进程根目录/config/ 下寻找
//fixed for issue #15 读取配置文件路径
realFile := configFile
if !_file.Exist(realFile) {
realFile = _file.GetCurrentDirectory() + "/" + configFile
if !_file.Exist(realFile) {
realFile = _file.GetCurrentDirectory() + "/config/" + configFile
if !_file.Exist(realFile) {
return nil, errors.New("no exists config file => " + configFile)
}
}
}
cType := ConfigType_Xml
config, err = initConfig(realFile, cType, fromXml)
if err != nil {
return config, err
}
return config, nil
}
func initConfig(configFile string, ctType string, f func([]byte, interface{}) error) (*AppConfig, error) {
content, err := ioutil.ReadFile(configFile)
if err != nil {
return nil, errors.New("GoLog:Config:initConfig 当前cType:" + ctType + " 配置文件[" + configFile + "]读取失败 - " + err.Error())
}
var config *AppConfig
err = f(content, &config)
if err != nil {
return nil, errors.New("GoLog:Config:initConfig 当前cType:" + ctType + " 配置文件[" + configFile + "]解析失败 - " + err.Error())
}
return config, nil
}