-
Notifications
You must be signed in to change notification settings - Fork 272
/
config.go
46 lines (35 loc) · 1.1 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
package logger
import (
"strings"
)
// Config describes an interface which provides a method for getting a logging
// level for a particular namespace.
type Config interface {
// LevelForNamespace returns a logging Level for particular namespace.
LevelForNamespace(namespace string) Level
}
// ConfigMap reads the configuration from a CSV string. For example it can
// be easily used for reading the configuration from an environment variable.
type ConfigMap map[string]Level
// NewConfigFromString parses the provided string and returns Config.
func NewConfigFromString(stringConfig string) Config {
if stringConfig == "" {
return nil
}
configSlice := strings.Split(stringConfig, ",")
config := make(ConfigMap, len(configSlice))
for _, ns := range strings.Split(stringConfig, ",") {
level := LevelInfo
if index := strings.LastIndex(ns, ":"); index > -1 {
if cfgLevel, ok := LevelFromString(ns[index+1:]); ok {
level = cfgLevel
ns = ns[:index]
}
}
config[ns] = level
}
return NewConfig(config)
}
func NewConfig(configMap ConfigMap) Config {
return newWildcardNode(configMap)
}