forked from mgutz/logxi
/
env.go
167 lines (148 loc) · 3.58 KB
/
env.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
package log
import (
"os"
"strconv"
"strings"
)
var contextLines int
// Configuration comes from environment or external services like
// consul, etcd.
type Configuration struct {
Format string `json:"format"`
Colors string `json:"colors"`
Levels string `json:"levels"`
}
func readFromEnviron() *Configuration {
conf := &Configuration{}
var envOrDefault = func(name, val string) string {
result := os.Getenv(name)
if result == "" {
result = val
}
return result
}
conf.Levels = envOrDefault("LOGXI", defaultLogxiEnv)
conf.Format = envOrDefault("LOGXI_FORMAT", defaultLogxiFormatEnv)
conf.Colors = envOrDefault("LOGXI_COLORS", defaultLogxiColorsEnv)
return conf
}
// ProcessEnv (re)processes environment.
func ProcessEnv(env *Configuration) {
// TODO: allow reading from etcd
ProcessLogxiEnv(env.Levels)
ProcessLogxiColorsEnv(env.Colors)
ProcessLogxiFormatEnv(env.Format)
}
// ProcessLogxiFormatEnv parses LOGXI_FORMAT
func ProcessLogxiFormatEnv(env string) {
logxiFormat = env
m := parseKVList(logxiFormat, ",")
formatterFormat := ""
tFormat := ""
for key, value := range m {
switch key {
default:
formatterFormat = key
case "t":
tFormat = value
case "pretty":
isPretty = value != "false" && value != "0"
case "maxcol":
col, err := strconv.Atoi(value)
if err == nil {
maxCol = col
} else {
maxCol = defaultMaxCol
}
case "context":
lines, err := strconv.Atoi(value)
if err == nil {
contextLines = lines
} else {
contextLines = defaultContextLines
}
case "LTSV":
formatterFormat = "text"
AssignmentChar = ltsvAssignmentChar
Separator = ltsvSeparator
}
}
if formatterFormat == "" || formatterCreators[formatterFormat] == nil {
formatterFormat = defaultFormat
}
logxiFormat = formatterFormat
if tFormat == "" {
tFormat = defaultTimeFormat
}
timeFormat = tFormat
}
// ProcessLogxiEnv parses LOGXI variable
func ProcessLogxiEnv(env string) {
logxiEnable := env
if logxiEnable == "" {
logxiEnable = defaultLogxiEnv
}
logxiNameLevelMap = map[string]int{}
m := parseKVList(logxiEnable, ",")
if m == nil {
logxiNameLevelMap["*"] = defaultLevel
}
for key, value := range m {
if strings.HasPrefix(key, "-") {
// LOGXI=*,-foo => disable foo
logxiNameLevelMap[key[1:]] = LevelOff
} else if value == "" {
// LOGXI=* => default to all
logxiNameLevelMap[key] = LevelAll
} else {
// LOGXI=*=ERR => use user-specified level
level := LevelAtoi[value]
if level == 0 {
InternalLog.Error("Unknown level in LOGXI environment variable", "key", key, "value", value, "LOGXI", env)
level = defaultLevel
}
logxiNameLevelMap[key] = level
}
}
// must always have global default, otherwise errs may get eaten up
if _, ok := logxiNameLevelMap["*"]; !ok {
logxiNameLevelMap["*"] = LevelError
}
}
func getLogLevel(name string) int {
var wildcardLevel int
var result int
for k, v := range logxiNameLevelMap {
if k == name {
result = v
break
} else if k == "*" {
wildcardLevel = v
} else if strings.HasPrefix(k, "*") && strings.HasSuffix(name, k[1:]) {
result = v
} else if strings.HasSuffix(k, "*") && strings.HasPrefix(name, k[:len(k)-1]) {
result = v
}
}
if result == LevelOff {
return LevelOff
}
if result > 0 {
return result
}
if wildcardLevel > 0 {
return wildcardLevel
}
return LevelOff
}
// ProcessLogxiColorsEnv parases LOGXI_COLORS
func ProcessLogxiColorsEnv(env string) {
colors := env
if colors == "" {
colors = defaultLogxiColorsEnv
} else if colors == "*=off" {
// disable all colors
disableColors = true
}
theme = parseTheme(colors)
}