This repository has been archived by the owner on Jun 5, 2019. It is now read-only.
/
config.go
69 lines (56 loc) · 2.07 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
package logging
import (
"fmt"
"strings"
)
type Config struct {
LogLevel string `env:"log_level" file:"log_level" default:"info"`
LogEncoding string `env:"log_encoding" file:"log_encoding" default:"console"`
LogColorize bool `env:"log_colorize" file:"log_colorize" default:"true"`
LogJSONFieldNames map[string]string `env:"log_json_field_names" file:"log_json_field_names"`
LogInitialFields Fields `env:"log_fields" file:"log_fields"`
LogShortTime bool `env:"log_short_time" file:"log_short_time" default:"false"`
LogDisplayFields bool `env:"log_display_fields" file:"log_display_fields" default:"true"`
LogDisplayMultilineFields bool `env:"log_display_multiline_fields" file:"log_display_multiline_fields" default:"false"`
RawLogFieldBlacklist string `env:"log_field_blacklist" file:"log_field_blacklist" mask:"true"`
LogFieldBlacklist []string
}
var (
ErrIllegalLevel = fmt.Errorf("illegal log level")
ErrIllegalEncoding = fmt.Errorf("illegal log encoding")
)
func (c *Config) PostLoad() error {
c.LogLevel = strings.ToLower(c.LogLevel)
if !isLegalLevel(c.LogLevel) {
return ErrIllegalLevel
}
if !isLegalEncoding(c.LogEncoding) {
return ErrIllegalEncoding
}
for name := range c.LogJSONFieldNames {
if !isLegalJSONFieldName(name) {
return fmt.Errorf("unknown JSON field name %s", name)
}
}
for _, s := range strings.Split(c.RawLogFieldBlacklist, ",") {
c.LogFieldBlacklist = append(
c.LogFieldBlacklist,
strings.ToLower(strings.TrimSpace(s)),
)
}
return nil
}
func isLegalLevel(level string) bool {
for _, whitelisted := range []string{"debug", "info", "warning", "error", "fatal"} {
if level == whitelisted {
return true
}
}
return false
}
func isLegalEncoding(encoding string) bool {
return encoding == "console" || encoding == "json"
}
func isLegalJSONFieldName(name string) bool {
return name == "message" || name == "timestamp" || name == "level"
}