/
config.go
145 lines (129 loc) · 4.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
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
// SPDX-FileCopyrightText: 2019 SAP SE or an SAP affiliate company and Gardener contributors.
//
// SPDX-License-Identifier: Apache-2.0
package logging
import (
flag "github.com/spf13/pflag"
"go.uber.org/zap"
)
type Config struct {
flagset *flag.FlagSet
Development bool
Cli bool
DisableStacktrace bool
DisableCaller bool
DisableTimestamp bool
Level logLevelValue
Format logFormatValue
}
func InitFlags(flagset *flag.FlagSet) {
if flagset == nil {
flagset = flag.CommandLine
}
fs := flag.NewFlagSet("log", flag.ExitOnError)
fs.BoolVar(&configFromFlags.Development, "dev", false, "enable development logging")
fs.BoolVar(&configFromFlags.Cli, "cli", false, "use CLI formatting for logs (color, no timestamps)")
f := fs.VarPF(&configFromFlags.Format, "format", "f", "logging format [text, json]")
f.DefValue = "text if either dev or cli flag is set, json otherwise"
f = fs.VarPF(&configFromFlags.Level, "verbosity", "v", "logging verbosity [error, info, debug]")
f.DefValue = "info, or debug if dev flag is set"
fs.BoolVar(&configFromFlags.DisableStacktrace, "disable-stacktrace", true, "disable the stacktrace of error logs")
fs.BoolVar(&configFromFlags.DisableCaller, "disable-caller", true, "disable the caller of logs")
fs.BoolVar(&configFromFlags.DisableTimestamp, "disable-timestamp", false, "disable timestamp output")
configFromFlags.flagset = fs
flagset.AddFlagSet(configFromFlags.flagset)
}
// SetLogLevel sets the logging verbosity according to the provided flag if the flag was provided
func (c *Config) SetLogLevel(zapCfg *zap.Config) {
if !c.Level.IsUnset() {
zapCfg.Level = zap.NewAtomicLevelAt(toZapLevel(c.Level.Value()))
}
}
// SetLogFormat sets the logging format according to the provided flag if the flag was provided
func (c *Config) SetLogFormat(zapCfg *zap.Config) {
if !c.Format.IsUnset() {
zapCfg.Encoding = toZapFormat(c.Format.Value())
}
}
// SetDisableStacktrace dis- or enables the stackstrace according to the provided flag if the flag was provided
func (c *Config) SetDisableStacktrace(zapCfg *zap.Config) {
if c.flagset != nil && c.flagset.Changed("disable-stacktrace") {
zapCfg.DisableStacktrace = c.DisableStacktrace
}
}
// SetDisableCaller dis- or enables the caller according to the provided flag if the flag was provided
func (c *Config) SetDisableCaller(zapCfg *zap.Config) {
if c.flagset != nil && c.flagset.Changed("disable-caller") {
zapCfg.DisableCaller = c.DisableCaller
}
}
// SetTimestamp dis- or enables the logging of timestamps according to the provided flag if the flag was provided
func (c *Config) SetTimestamp(zapCfg *zap.Config) {
if c.flagset != nil && c.flagset.Changed("disable-timestamp") {
if c.DisableTimestamp {
zapCfg.EncoderConfig.TimeKey = ""
} else {
zapCfg.EncoderConfig.TimeKey = "ts"
}
}
}
// logLevelValue implements the Value interface for LogLevel
type logLevelValue struct {
internal LogLevel
}
func (l *logLevelValue) String() string {
return l.internal.String()
}
func (l *logLevelValue) Set(raw string) error {
lvl, err := ParseLogLevel(raw)
if err != nil {
return err
}
l.internal = lvl
return nil
}
func (l *logLevelValue) Type() string {
return "LogLevel"
}
func (l *logLevelValue) Value() LogLevel {
return l.internal
}
func (l *logLevelValue) IsUnset() bool {
return l.internal == unknown_level
}
func (c *Config) WithLogLevel(l LogLevel) *Config {
c.Level = logLevelValue{
internal: l,
}
return c
}
// logFormatValue implements the Value interface for LogFormat
type logFormatValue struct {
internal LogFormat
}
func (l *logFormatValue) String() string {
return l.internal.String()
}
func (l *logFormatValue) Set(raw string) error {
f, err := ParseLogFormat(raw)
if err != nil {
return err
}
l.internal = f
return nil
}
func (l *logFormatValue) Type() string {
return "LogFormat"
}
func (l *logFormatValue) Value() LogFormat {
return l.internal
}
func (l *logFormatValue) IsUnset() bool {
return l.internal == unknown_format
}
func (c *Config) WithLogFormat(f LogFormat) *Config {
c.Format = logFormatValue{
internal: f,
}
return c
}