/
logger.go
78 lines (74 loc) · 2.21 KB
/
logger.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
package logger
import (
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"k8s.io/cli-runtime/pkg/genericclioptions"
)
// New creates a logger with the corresponding format and level
func New(streams genericclioptions.IOStreams) *zap.Logger {
// High-priority output should go to standard error, and low-priority
// output should go to standard out.
consoleDebugging := zapcore.AddSync(streams.Out)
consoleErrors := zapcore.AddSync(streams.ErrOut)
cores := []zapcore.Core{}
ll := viper.GetString("log-level")
// Set default level to "info"
if ll != "debug" && ll != "warn" && ll != "error" {
ll = "info"
}
switch ll {
case "debug":
cores = append(cores,
zapcore.NewCore(getZapEncoder(),
consoleDebugging,
zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
return lev == zapcore.DebugLevel
})))
fallthrough
case "info":
cores = append(cores,
zapcore.NewCore(getZapEncoder(),
consoleDebugging,
zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
return lev == zapcore.InfoLevel
})))
fallthrough
case "warn":
cores = append(cores,
zapcore.NewCore(getZapEncoder(),
consoleDebugging,
zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
return lev == zapcore.WarnLevel
})))
fallthrough
case "error":
cores = append(cores,
zapcore.NewCore(getZapEncoder(),
consoleErrors,
zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
return lev >= zapcore.ErrorLevel
})))
}
return zap.New(zapcore.NewTee(cores...), zap.AddCaller())
}
// getZapEncodingConfig returns the configuration of zap encoder.
func getZapEncodingConfig() zapcore.EncoderConfig {
return zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "msg",
StacktraceKey: "",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalColorLevelEncoder,
EncodeTime: zapcore.TimeEncoderOfLayout("15:04:05"),
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
}
// getZapEncoder returns the zap encoder.
func getZapEncoder() zapcore.Encoder {
return zapcore.NewConsoleEncoder(getZapEncodingConfig())
}