-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
64 lines (54 loc) · 1.17 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
package logri
import (
"bytes"
"errors"
"io"
"sort"
"strings"
"gopkg.in/yaml.v2"
)
var (
ConfigurationError = errors.New("Unable to parse configuration")
)
// LogriConfig is the configuration for a logri manager
type LogriConfig []LoggerConfig
// LoggerConfig is the configuration for a single logger
type LoggerConfig struct {
Logger string
Level string
Local bool
Out []OutConfig
}
type OutConfig struct {
Type OutputType
Options map[string]string
Local bool
}
func ConfigFromBytes(b []byte) (LogriConfig, error) {
var (
cfg LogriConfig
)
if err := yaml.Unmarshal(b, &cfg); err != nil {
return cfg, err
}
sort.Sort(&cfg)
return cfg, nil
}
func ConfigFromYAML(r io.Reader) (LogriConfig, error) {
var buf bytes.Buffer
buf.ReadFrom(r)
return ConfigFromBytes(buf.Bytes())
}
func (c LogriConfig) Len() int { return len(c) }
func (c LogriConfig) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
// Sort loggers by depth in the hierarchy
func (c LogriConfig) Less(i, j int) bool {
a, b := c[i].Logger, c[j].Logger
if a == "*" || a == "" {
return true
}
if b == "*" || b == "" {
return false
}
return strings.Count(a, ".") < strings.Count(b, ".")
}