-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathyaml.go
More file actions
231 lines (206 loc) · 5.95 KB
/
Copy pathyaml.go
File metadata and controls
231 lines (206 loc) · 5.95 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
package config
import (
"fmt"
"os"
"gopkg.in/yaml.v3"
)
// yamlConfig YAML 配置结构
type yamlConfig struct {
Global globalConfig `yaml:"global"`
Variables []variableConfig `yaml:"variables"`
Targets targetList `yaml:"targets"`
Loggers []loggerConfig `yaml:"loggers"`
}
type globalConfig struct {
IsLog bool `yaml:"isLog"`
ChanSize int `yaml:"chanSize"`
InnerLogPath string `yaml:"innerLogPath"`
InnerLogEncode string `yaml:"innerLogEncode"`
}
type variableConfig struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
}
type targetList struct {
File []fileTargetConfig `yaml:"file"`
Udp []udpTargetConfig `yaml:"udp"`
Http []httpTargetConfig `yaml:"http"`
EMail []emailTargetConfig `yaml:"email"`
Fmt []fmtTargetConfig `yaml:"fmt"`
JSON []JSONTargetConfig `yaml:"json"`
}
type fileTargetConfig struct {
Name string `yaml:"name"`
IsLog bool `yaml:"isLog"`
Layout string `yaml:"layout"`
Encode string `yaml:"encode"`
FileMaxSize int64 `yaml:"fileMaxSize"`
FileName string `yaml:"fileName"`
MaxBackups int `yaml:"maxBackups"` // 保留备份文件数量,0 表示不清理
}
type udpTargetConfig struct {
Name string `yaml:"name"`
IsLog bool `yaml:"isLog"`
Layout string `yaml:"layout"`
Encode string `yaml:"encode"`
RemoteIP string `yaml:"remoteIP"`
}
type httpTargetConfig struct {
Name string `yaml:"name"`
IsLog bool `yaml:"isLog"`
Layout string `yaml:"layout"`
Encode string `yaml:"encode"`
HttpUrl string `yaml:"httpUrl"`
}
type emailTargetConfig struct {
Name string `yaml:"name"`
IsLog bool `yaml:"isLog"`
Layout string `yaml:"layout"`
Encode string `yaml:"encode"`
MailServer string `yaml:"mailServer"`
MailAccount string `yaml:"mailAccount"`
MailNickName string `yaml:"mailNickName"`
MailPassword string `yaml:"mailPassword"`
ToMail string `yaml:"toMail"`
Subject string `yaml:"subject"`
}
type fmtTargetConfig struct {
Name string `yaml:"name"`
IsLog bool `yaml:"isLog"`
Layout string `yaml:"layout"`
Encode string `yaml:"encode"`
}
type loggerConfig struct {
Name string `yaml:"name"`
IsLog bool `yaml:"isLog"`
Layout string `yaml:"layout"`
ConfigMode string `yaml:"configMode"`
Levels []loggerLevelConfig `yaml:"levels"`
}
type loggerLevelConfig struct {
Level string `yaml:"level"`
Targets string `yaml:"targets"`
IsLog bool `yaml:"isLog"`
}
// LoadYamlConfig loads configuration from YAML file
func LoadYamlConfig(configFile string) (*AppConfig, error) {
data, err := os.ReadFile(configFile)
if err != nil {
return nil, fmt.Errorf("failed to read config file: %w", err)
}
var yc yamlConfig
if err := yaml.Unmarshal(data, &yc); err != nil {
return nil, fmt.Errorf("failed to parse YAML config: %w", err)
}
// Convert YAML config to AppConfig
appConfig := &AppConfig{
Global: &GlobalConfig{
IsLog: yc.Global.IsLog,
ChanSize: yc.Global.ChanSize,
InnerLogPath: yc.Global.InnerLogPath,
InnerLogEncode: yc.Global.InnerLogEncode,
},
Variables: make([]*VariableConfig, len(yc.Variables)),
Loggers: make([]*LoggerConfig, len(yc.Loggers)),
Targets: &TargetList{
FileTargets: make([]*FileTargetConfig, len(yc.Targets.File)),
UdpTargets: make([]*UdpTargetConfig, len(yc.Targets.Udp)),
HttpTargets: make([]*HttpTargetConfig, len(yc.Targets.Http)),
EMailTargets: make([]*EMailTargetConfig, len(yc.Targets.EMail)),
FmtTargets: make([]*FmtTargetConfig, len(yc.Targets.Fmt)),
JSONTargets: make([]*JSONTargetConfig, len(yc.Targets.JSON)),
},
}
// Convert variables
for i, v := range yc.Variables {
appConfig.Variables[i] = &VariableConfig{Name: v.Name, Value: v.Value}
}
// Convert loggers
for i, l := range yc.Loggers {
levels := make([]*LoggerLevelConfig, len(l.Levels))
for j, level := range l.Levels {
levels[j] = &LoggerLevelConfig{
Level: level.Level,
Targets: level.Targets,
IsLog: level.IsLog,
}
}
appConfig.Loggers[i] = &LoggerConfig{
Name: l.Name,
IsLog: l.IsLog,
Layout: l.Layout,
ConfigMode: l.ConfigMode,
Levels: levels,
}
}
// Convert file targets
for i, t := range yc.Targets.File {
appConfig.Targets.FileTargets[i] = &FileTargetConfig{
Name: t.Name,
IsLog: t.IsLog,
Layout: t.Layout,
Encode: t.Encode,
FileMaxSize: t.FileMaxSize,
FileName: t.FileName,
MaxBackups: t.MaxBackups,
}
}
// Convert UDP targets
for i, t := range yc.Targets.Udp {
appConfig.Targets.UdpTargets[i] = &UdpTargetConfig{
Name: t.Name,
IsLog: t.IsLog,
Layout: t.Layout,
Encode: t.Encode,
RemoteIP: t.RemoteIP,
}
}
// Convert HTTP targets
for i, t := range yc.Targets.Http {
appConfig.Targets.HttpTargets[i] = &HttpTargetConfig{
Name: t.Name,
IsLog: t.IsLog,
Layout: t.Layout,
Encode: t.Encode,
HttpUrl: t.HttpUrl,
}
}
// Convert Email targets
for i, t := range yc.Targets.EMail {
appConfig.Targets.EMailTargets[i] = &EMailTargetConfig{
Name: t.Name,
IsLog: t.IsLog,
Layout: t.Layout,
Encode: t.Encode,
MailServer: t.MailServer,
MailAccount: t.MailAccount,
MailNickName: t.MailNickName,
MailPassword: t.MailPassword,
ToMail: t.ToMail,
Subject: t.Subject,
}
}
// Convert Fmt targets
for i, t := range yc.Targets.Fmt {
appConfig.Targets.FmtTargets[i] = &FmtTargetConfig{
Name: t.Name,
IsLog: t.IsLog,
Layout: t.Layout,
Encode: t.Encode,
}
}
// Convert JSON targets
for i, t := range yc.Targets.JSON {
appConfig.Targets.JSONTargets[i] = &JSONTargetConfig{
Name: t.Name,
IsLog: t.IsLog,
Layout: t.Layout,
Encode: t.Encode,
FileName: t.FileName,
FileMaxSize: t.FileMaxSize,
MaxBackups: t.MaxBackups,
PrettyPrint: t.PrettyPrint,
}
}
return appConfig, nil
}