-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
71 lines (61 loc) · 1.69 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
// SPDX-FileCopyrightText: 2022-present Open Networking Foundation <[email protected]>
//
// SPDX-License-Identifier: Apache-2.0
package dazl
import (
"github.com/mitchellh/go-homedir"
"gopkg.in/yaml.v3"
"os"
"path/filepath"
)
const configFile = "logging.yaml"
const configEnv = "LOGGING_CONFIG"
type loggingConfig struct {
Encoders encodersConfig `json:"encoders" yaml:"encoders"`
Writers writersConfig `json:"writers" yaml:"writers"`
RootLogger loggerConfig `json:"rootLogger" yaml:"rootLogger"`
Loggers map[string]loggerConfig `json:"loggers" yaml:"loggers"`
}
func (c *loggingConfig) getLoggers() map[string]loggerConfig {
if c.Loggers == nil {
return map[string]loggerConfig{}
}
return c.Loggers
}
func (c *loggingConfig) getLogger(name string) (loggerConfig, bool) {
config, ok := c.getLoggers()[name]
return config, ok
}
// load the dazl configuration
func load(config *loggingConfig) error {
configPath := os.Getenv(configEnv)
if configPath != "" {
bytes, err := os.ReadFile(configPath)
if err == nil {
return yaml.Unmarshal(bytes, config)
} else if !os.IsNotExist(err) {
return err
}
}
bytes, err := os.ReadFile(configFile)
if err == nil {
return yaml.Unmarshal(bytes, config)
} else if !os.IsNotExist(err) {
return err
}
if home, err := homedir.Dir(); err == nil {
bytes, err = os.ReadFile(filepath.Join(home, configFile))
if err == nil {
return yaml.Unmarshal(bytes, config)
} else if !os.IsNotExist(err) {
return err
}
}
bytes, err = os.ReadFile(filepath.Join("/etc/dazl", configFile))
if err == nil {
return yaml.Unmarshal(bytes, config)
} else if !os.IsNotExist(err) {
return err
}
return nil
}