-
Notifications
You must be signed in to change notification settings - Fork 32
/
config.go
304 lines (263 loc) · 7.18 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright 2020-present Open Networking Foundation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logging
import (
"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)
const configDir = ".onos"
// SinkType is the type of a sink
type SinkType string
func (t SinkType) String() string {
return string(t)
}
const (
// StdoutSinkType is the sink type for stdout
StdoutSinkType SinkType = "stdout"
// StderrSinkType is the sink type for stderr
StderrSinkType SinkType = "stderr"
// FileSinkType is the type for a file sink
FileSinkType SinkType = "file"
// KafkaSinkType is the sink type for the Kafka sink
KafkaSinkType SinkType = "kafka"
)
// SinkEncoding is the encoding for a sink
type SinkEncoding string
func (e SinkEncoding) String() string {
return string(e)
}
const (
// ConsoleEncoding is an encoding for outputs to the console
ConsoleEncoding SinkEncoding = "console"
// JSONEncoding is an encoding for JSON outputs
JSONEncoding SinkEncoding = "json"
)
const (
rootLoggerName = "root"
)
// Config logging configuration
type Config struct {
Loggers map[string]LoggerConfig `yaml:"loggers"`
Sinks map[string]SinkConfig `yaml:"sinks"`
}
// GetRootLogger returns the root logger configuration
func (c Config) GetRootLogger() LoggerConfig {
root, ok := c.Loggers[rootLoggerName]
if ok {
if root.Output == nil {
root.Output = map[string]OutputConfig{}
}
return root
}
return LoggerConfig{Output: map[string]OutputConfig{}}
}
// GetLoggers returns the configured loggers
func (c Config) GetLoggers() []LoggerConfig {
loggers := make([]LoggerConfig, 0, len(c.Loggers))
for name, logger := range c.Loggers {
if name != rootLoggerName {
logger.Name = name
if logger.Output == nil {
logger.Output = map[string]OutputConfig{}
}
loggers = append(loggers, logger)
}
}
return loggers
}
// GetLogger returns a logger by name
func (c Config) GetLogger(name string) (LoggerConfig, bool) {
if name == rootLoggerName {
return LoggerConfig{}, false
}
logger, ok := c.Loggers[name]
if ok {
logger.Name = name
if logger.Output == nil {
logger.Output = map[string]OutputConfig{}
}
return logger, true
}
return LoggerConfig{Output: map[string]OutputConfig{}}, false
}
// GetSinks returns the configured sinks
func (c Config) GetSinks() []SinkConfig {
sinks := make([]SinkConfig, 0, len(c.Sinks))
for name, sink := range c.Sinks {
sink.Name = name
sinks = append(sinks, sink)
}
return sinks
}
// GetSink returns a sink by name
func (c Config) GetSink(name string) (SinkConfig, bool) {
sink, ok := c.Sinks[name]
if ok {
sink.Name = name
return sink, true
}
return SinkConfig{}, false
}
// LoggerConfig is the configuration for a logger
type LoggerConfig struct {
Name string `yaml:"name"`
Level *string `yaml:"level,omitempty"`
Output map[string]OutputConfig `yaml:"output"`
}
// GetLevel returns the logger level
func (c LoggerConfig) GetLevel() Level {
level := c.Level
if level != nil {
return levelStringToLevel(*level)
}
return ErrorLevel
}
// GetOutputs returns the logger outputs
func (c LoggerConfig) GetOutputs() []OutputConfig {
outputs := c.Output
outputsList := make([]OutputConfig, 0, len(outputs))
for name, output := range outputs {
output.Name = name
outputsList = append(outputsList, output)
}
return outputsList
}
// GetOutput returns an output by name
func (c LoggerConfig) GetOutput(name string) (OutputConfig, bool) {
output, ok := c.Output[name]
if ok {
output.Name = name
return output, true
}
return OutputConfig{}, false
}
// OutputConfig is the configuration for a sink output
type OutputConfig struct {
Name string `yaml:"name"`
Sink *string `yaml:"sink"`
Level *string `yaml:"level,omitempty"`
}
// GetSink returns the output sink
func (c OutputConfig) GetSink() string {
sink := c.Sink
if sink != nil {
return *sink
}
return ""
}
// GetLevel returns the output level
func (c OutputConfig) GetLevel() Level {
level := c.Level
if level != nil {
return levelStringToLevel(*level)
}
return DebugLevel
}
// SinkConfig is the configuration for a sink
type SinkConfig struct {
Name string `yaml:"name"`
Type *SinkType `yaml:"type,omitempty"`
Encoding *SinkEncoding `yaml:"encoding,omitempty"`
Stdout *StdoutSinkConfig `yaml:"stdout,omitempty"`
Stderr *StderrSinkConfig `yaml:"stderr,omitempty"`
File *FileSinkConfig `yaml:"file,omitempty"`
Kafka *KafkaSinkConfig `yaml:"kafka,omitempty"`
}
// GetType returns the sink type
func (c SinkConfig) GetType() SinkType {
sinkType := c.Type
if sinkType != nil {
return *sinkType
}
return StdoutSinkType
}
// GetEncoding returns the sink encoding
func (c SinkConfig) GetEncoding() SinkEncoding {
encoding := c.Encoding
if encoding != nil {
return *encoding
}
return ConsoleEncoding
}
// GetStdoutSinkConfig returns the stdout sink configuration
func (c SinkConfig) GetStdoutSinkConfig() StdoutSinkConfig {
config := c.Stdout
if config != nil {
return *config
}
return StdoutSinkConfig{}
}
// GetStderrSinkConfig returns the stderr sink configuration
func (c SinkConfig) GetStderrSinkConfig() StderrSinkConfig {
config := c.Stderr
if config != nil {
return *config
}
return StderrSinkConfig{}
}
// GetFileSinkConfig returns the file sink configuration
func (c SinkConfig) GetFileSinkConfig() FileSinkConfig {
config := c.File
if config != nil {
return *config
}
return FileSinkConfig{}
}
// GetKafkaSinkConfig returns the Kafka sink configuration
func (c SinkConfig) GetKafkaSinkConfig() KafkaSinkConfig {
config := c.Kafka
if config != nil {
return *config
}
return KafkaSinkConfig{}
}
// StdoutSinkConfig is the configuration for an stdout sink
type StdoutSinkConfig struct {
}
// StderrSinkConfig is the configuration for an stderr sink
type StderrSinkConfig struct {
}
// FileSinkConfig is the configuration for a file sink
type FileSinkConfig struct {
Path string `yaml:"path"`
}
// KafkaSinkConfig is the configuration for a Kafka sink
type KafkaSinkConfig struct {
Topic string `yaml:"topic"`
Key string `yaml:"key"`
Brokers []string `yaml:"brokers"`
}
// load loads the configuration
func load(config *Config) error {
home, err := homedir.Dir()
if err != nil {
return err
}
// Set the file name of the configurations file
viper.SetConfigName("logging")
// Set the path to look for the configurations file
viper.AddConfigPath("./" + configDir + "/config")
viper.AddConfigPath(home + "/" + configDir + "/config")
viper.AddConfigPath("/etc/onos/config")
viper.AddConfigPath(".")
viper.SetConfigType("yaml")
if err := viper.ReadInConfig(); err != nil {
return nil
}
err = viper.Unmarshal(config)
if err != nil {
return err
}
return nil
}