-
Notifications
You must be signed in to change notification settings - Fork 332
/
Copy pathconfig.go
276 lines (233 loc) · 7.66 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
Copyright 2018 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package logging
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"github.com/blendle/zapdriver"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
corev1 "k8s.io/api/core/v1"
"knative.dev/pkg/changeset"
"knative.dev/pkg/logging/logkey"
)
const (
configMapNameEnv = "CONFIG_LOGGING_NAME"
loggerConfigKey = "zap-logger-config"
fallbackLoggerName = "fallback-logger"
)
var (
errEmptyLoggerConfig = errors.New("empty logger configuration")
errEmptyJSONLogginString = errors.New("json logging string is empty")
)
// NewLogger creates a logger with the supplied configuration.
// In addition to the logger, it returns AtomicLevel that can
// be used to change the logging level at runtime.
// If configuration is empty, a fallback configuration is used.
// If configuration cannot be used to instantiate a logger,
// the same fallback configuration is used.
func NewLogger(configJSON string, levelOverride string, opts ...zap.Option) (*zap.SugaredLogger, zap.AtomicLevel) {
logger, atomicLevel, err := newLoggerFromConfig(configJSON, levelOverride, opts)
if err == nil {
return enrichLoggerWithCommitID(logger), atomicLevel
}
loggingCfg := stackdriverConfig()
if levelOverride != "" {
if level, err := levelFromString(levelOverride); err == nil {
loggingCfg.Level = zap.NewAtomicLevelAt(*level)
}
}
logger, err = loggingCfg.Build(opts...)
if err != nil {
panic(err)
}
slogger := enrichLoggerWithCommitID(logger.Named(fallbackLoggerName))
slogger.Warnw("Failed to parse logging config - using default zap production config", zap.Error(err))
return slogger, loggingCfg.Level
}
func enrichLoggerWithCommitID(logger *zap.Logger) *zap.SugaredLogger {
revision := changeset.Get()
if revision == changeset.Unknown {
logger.Info("Unable to read vcs.revision from binary")
return logger.Sugar()
}
// Enrich logs with the components git revision.
return logger.With(zap.String(logkey.Commit, revision)).Sugar()
}
// NewLoggerFromConfig creates a logger using the provided Config
func NewLoggerFromConfig(config *Config, name string, opts ...zap.Option) (*zap.SugaredLogger, zap.AtomicLevel) {
var componentLvl string
if lvl, defined := config.LoggingLevel[name]; defined {
componentLvl = lvl.String()
}
logger, level := NewLogger(config.LoggingConfig, componentLvl, opts...)
return logger.Named(name), level
}
func newLoggerFromConfig(configJSON, levelOverride string, opts []zap.Option) (*zap.Logger, zap.AtomicLevel, error) {
loggingCfg, err := zapConfigFromJSON(configJSON)
if err != nil {
return nil, zap.AtomicLevel{}, err
}
if levelOverride != "" {
if level, err := levelFromString(levelOverride); err == nil {
loggingCfg.Level = zap.NewAtomicLevelAt(*level)
}
}
logger, err := loggingCfg.Build(opts...)
if err != nil {
return nil, zap.AtomicLevel{}, err
}
logger.Debug("Successfully created the logger.")
logger.Debug("Logging level set to: " + loggingCfg.Level.String())
return logger, loggingCfg.Level, nil
}
func zapConfigFromJSON(configJSON string) (*zap.Config, error) {
loggingCfg := stackdriverConfig()
if configJSON != "" {
if err := json.Unmarshal([]byte(configJSON), &loggingCfg); err != nil {
return nil, err
}
}
return &loggingCfg, nil
}
// Config contains the configuration defined in the logging ConfigMap.
// +k8s:deepcopy-gen=true
type Config struct {
LoggingConfig string
LoggingLevel map[string]zapcore.Level
}
type lcfg struct{}
// WithConfig associates a logging configuration with the context.
func WithConfig(ctx context.Context, cfg *Config) context.Context {
return context.WithValue(ctx, lcfg{}, cfg)
}
// GetConfig gets the logging config from the provided context.
func GetConfig(ctx context.Context) *Config {
untyped := ctx.Value(lcfg{})
if untyped == nil {
return nil
}
return untyped.(*Config)
}
func defaultConfig() *Config {
return &Config{
LoggingLevel: make(map[string]zapcore.Level),
}
}
// NewConfigFromMap creates a LoggingConfig from the supplied map,
// expecting the given list of components.
func NewConfigFromMap(data map[string]string) (*Config, error) {
lc := defaultConfig()
if zlc, ok := data[loggerConfigKey]; ok {
lc.LoggingConfig = zlc
}
for k, v := range data {
if component := strings.TrimPrefix(k, "loglevel."); component != k && component != "" {
if len(v) > 0 {
level, err := levelFromString(v)
if err != nil {
return nil, err
}
lc.LoggingLevel[component] = *level
}
}
}
return lc, nil
}
// NewConfigFromConfigMap creates a Config from the supplied ConfigMap,
// expecting the given list of components.
func NewConfigFromConfigMap(configMap *corev1.ConfigMap) (*Config, error) {
return NewConfigFromMap(configMap.Data)
}
func levelFromString(level string) (*zapcore.Level, error) {
var zapLevel zapcore.Level
if err := zapLevel.UnmarshalText([]byte(level)); err != nil {
return nil, fmt.Errorf("invalid logging level: %v", level)
}
return &zapLevel, nil
}
// UpdateLevelFromConfigMap returns a helper func that can be used to update the logging level
// when a config map is updated
func UpdateLevelFromConfigMap(logger *zap.SugaredLogger, atomicLevel zap.AtomicLevel,
levelKey string,
) func(configMap *corev1.ConfigMap) {
return func(configMap *corev1.ConfigMap) {
config, err := NewConfigFromConfigMap(configMap)
if err != nil {
logger.Errorw("Failed to parse the logging configmap. Previous config map will be used.", zap.Error(err))
return
}
level, defined := config.LoggingLevel[levelKey]
if !defined {
// reset to global level
loggingCfg, err := zapConfigFromJSON(config.LoggingConfig)
switch {
case errors.Is(err, errEmptyLoggerConfig):
level = zap.NewAtomicLevel().Level()
case err != nil:
logger.Errorw("Failed to parse logger configuration. Previous log level retained for "+levelKey,
zap.Error(err))
return
default:
level = loggingCfg.Level.Level()
}
}
if atomicLevel.Level() != level {
logger.Infof("Updating logging level for %v from %v to %v.", levelKey, atomicLevel.Level(), level)
atomicLevel.SetLevel(level)
}
}
}
// ConfigMapName gets the name of the logging ConfigMap
func ConfigMapName() string {
if cm := os.Getenv(configMapNameEnv); cm != "" {
return cm
}
return "config-logging"
}
// JSONToConfig converts a JSON string of a Config.
// Always returns a non-nil Config.
func JSONToConfig(jsonCfg string) (*Config, error) {
if jsonCfg == "" {
return nil, errEmptyJSONLogginString
}
var configMap map[string]string
if err := json.Unmarshal([]byte(jsonCfg), &configMap); err != nil {
return nil, err
}
cfg, err := NewConfigFromMap(configMap)
if err != nil {
// Get the default config from logging package.
return NewConfigFromConfigMap(nil)
}
return cfg, nil
}
// ConfigToJSON converts a Config to a JSON string.
func ConfigToJSON(cfg *Config) (string, error) {
if cfg == nil || cfg.LoggingConfig == "" {
return "", nil
}
jsonCfg, err := json.Marshal(map[string]string{
loggerConfigKey: cfg.LoggingConfig,
})
return string(jsonCfg), err
}
func stackdriverConfig() zap.Config {
cfg := zapdriver.NewProductionConfig()
cfg.EncoderConfig.EncodeDuration = zapcore.StringDurationEncoder
return cfg
}