-
Notifications
You must be signed in to change notification settings - Fork 31
/
config.go
139 lines (118 loc) · 3.43 KB
/
config.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
// Copyright (c) 2015, huangjunwei <huangjunwei@youmi.net>. All rights reserved.
package blog4go
import (
"encoding/xml"
"errors"
"io/ioutil"
"os"
)
const (
// TypeTimeBaseRotate is time base logrotate tag
TypeTimeBaseRotate = "time"
// TypeSizeBaseRotate is size base logrotate tag
TypeSizeBaseRotate = "size"
)
var (
// ErrConfigFiltersNotFound not found filters
ErrConfigFiltersNotFound = errors.New("Please define at least one filter")
// ErrConfigBadAttributes wrong attribute
ErrConfigBadAttributes = errors.New("Bad attributes setting")
// ErrConfigLevelsNotFound not found levels
ErrConfigLevelsNotFound = errors.New("Please define levels attribution")
// ErrConfigFilePathNotFound not found file path
ErrConfigFilePathNotFound = errors.New("Please define the file path")
// ErrConfigFileRotateTypeNotFound not found rotate type
ErrConfigFileRotateTypeNotFound = errors.New("Please define the file rotate type")
// ErrConfigSocketAddressNotFound not found socket address
ErrConfigSocketAddressNotFound = errors.New("Please define a socket address")
// ErrConfigSocketNetworkNotFound not found socket port
ErrConfigSocketNetworkNotFound = errors.New("Please define a socket network type")
)
// Config struct define the config struct used for file wirter
type Config struct {
Filters []filter `xml:"filter"`
MinLevel string `xml:"minlevel,attr"`
}
// log filter
type filter struct {
Levels string `xml:"levels,attr"`
Colored bool `xml:"colored,attr"`
File file `xml:"file"`
RotateFile rotateFile `xml:"rotatefile"`
Console console `xml:"console"`
Socket socket `xml:"socket"`
}
type file struct {
Path string `xml:"path,attr"`
}
type rotateFile struct {
Path string `xml:"path,attr"`
Type string `xml:"type,attr"`
RotateLines int `xml:"rotateLines,attr"`
RotateSize int64 `xml:"rotateSize,attr"`
Retentions int64 `xml:"retentions,attr"`
}
type console struct {
// redirect stderr to stdout
Redirect bool `xml:"redirect"`
}
type socket struct {
Network string `xml:"network,attr"`
Address string `xml:"address,attr"`
}
// check if config is valid
func (config *Config) valid() error {
// check minlevel validation
if "" != config.MinLevel && !LevelFromString(config.MinLevel).valid() {
return ErrConfigBadAttributes
}
// check filters len
if len(config.Filters) < 1 {
return ErrConfigFiltersNotFound
}
// check filter one by one
for _, filter := range config.Filters {
if "" == filter.Levels {
return ErrConfigLevelsNotFound
}
if (file{}) != filter.File {
// seem not needed now
//if "" == filter.File.Path {
//return ErrConfigFilePathNotFound
//}
} else if (rotateFile{}) != filter.RotateFile {
if "" == filter.RotateFile.Path {
return ErrConfigFilePathNotFound
}
if "" == filter.RotateFile.Type {
return ErrConfigFileRotateTypeNotFound
}
} else if (socket{}) != filter.Socket {
if "" == filter.Socket.Address {
return ErrConfigSocketAddressNotFound
}
if "" == filter.Socket.Network {
return ErrConfigSocketNetworkNotFound
}
}
}
return nil
}
// read config from a xml file
func readConfig(fileName string) (*Config, error) {
file, err := os.Open(fileName)
if nil != err {
return nil, err
}
defer file.Close()
in, err := ioutil.ReadAll(file)
if nil != err {
return nil, err
}
config := new(Config)
err = xml.Unmarshal(in, config)
if nil != err {
return nil, err
}
return config, err
}