/
implementation.go
170 lines (150 loc) · 3.87 KB
/
implementation.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
168
169
170
// SPDX-FileCopyrightText: 2019 SAP SE or an SAP affiliate company and Gardener contributors.
//
// SPDX-License-Identifier: Apache-2.0
package logging
import (
"github.com/go-logr/zapr"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
Log Logger
configFromFlags = Config{}
)
func encoderConfig() zapcore.EncoderConfig {
return zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
}
func applyCLIEncoding(ecfg zapcore.EncoderConfig) zapcore.EncoderConfig {
ecfg.TimeKey = ""
ecfg.EncodeLevel = zapcore.LowercaseColorLevelEncoder
return ecfg
}
func defaultConfig() zap.Config {
return zap.Config{
Level: zap.NewAtomicLevelAt(toZapLevel(INFO)),
Development: false,
Encoding: toZapFormat(TEXT),
DisableStacktrace: true,
DisableCaller: true,
EncoderConfig: encoderConfig(),
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
}
}
func applyCLIConfig(cfg zap.Config) zap.Config {
cfg.EncoderConfig = applyCLIEncoding(cfg.EncoderConfig)
return cfg
}
func applyDevConfig(cfg zap.Config) zap.Config {
cfg.DisableCaller = false
cfg.DisableStacktrace = false
cfg.Development = true
cfg.Level = zap.NewAtomicLevelAt(toZapLevel(DEBUG))
return cfg
}
func applyProductionConfig(cfg zap.Config) zap.Config {
cfg.Encoding = toZapFormat(JSON)
return cfg
}
func New(config *Config) (Logger, error) {
if config == nil {
config = &configFromFlags
}
zapCfg := determineZapConfig(config)
zapLog, err := zapCfg.Build(zap.AddCallerSkip(1))
if err != nil {
return Logger{}, err
}
return Wrap(PreventKeyConflicts(zapr.NewLogger(zapLog))), nil
}
// GetLogger returns a singleton logger.
// Will initialize a new logger, if it doesn't exist yet.
func GetLogger() (Logger, error) {
if Log.IsInitialized() {
return Log, nil
}
log, err := New(nil)
if err != nil {
return Logger{}, err
}
SetLogger(log)
return log, nil
}
func SetLogger(log Logger) {
Log = log
}
// NewCliLogger creates a new logger for cli usage.
// CLI usage means that by default:
// - encoding is console
// - timestamps are disabled (can be still activated by the cli flag)
// - level are color encoded
func NewCliLogger() (Logger, error) {
config := &configFromFlags
config.Cli = true
return New(config)
}
func determineZapConfig(loggerConfig *Config) zap.Config {
zapConfig := defaultConfig()
if loggerConfig.Cli || loggerConfig.Development {
if loggerConfig.Cli {
zapConfig = applyCLIConfig(zapConfig)
}
if loggerConfig.Development {
zapConfig = applyDevConfig(zapConfig)
}
} else {
zapConfig = applyProductionConfig(zapConfig)
}
loggerConfig.SetLogLevel(&zapConfig)
loggerConfig.SetLogFormat(&zapConfig)
loggerConfig.SetDisableCaller(&zapConfig)
loggerConfig.SetDisableStacktrace(&zapConfig)
loggerConfig.SetTimestamp(&zapConfig)
return zapConfig
}
func levelToVerbosity(level LogLevel) int {
var res int
switch level {
case DEBUG:
res = int(zap.DebugLevel)
case ERROR:
res = int(zap.ErrorLevel)
default:
res = int(zap.InfoLevel)
}
return res * -1
}
// toZapLevel converts our LogLevel into a zap Level.
// Unknown LogLevels are silently treated as INFO.
func toZapLevel(l LogLevel) zapcore.Level {
switch l {
case DEBUG:
return zap.DebugLevel
case ERROR:
return zap.ErrorLevel
default:
return zap.InfoLevel
}
}
// toZapFormat converts our LogFormat into a zap encoding.
// Unknown LogFormats are silently treated as TEXT.
func toZapFormat(f LogFormat) string {
switch f {
case JSON:
return "json"
default:
return "console"
}
}