Skip to content

Commit

Permalink
fix(log): write error messages to stderr (aquasecurity#538)
Browse files Browse the repository at this point in the history
  • Loading branch information
knqyf263 authored Jun 23, 2020
1 parent 2ac672a commit 9dc1bdf
Showing 1 changed file with 44 additions and 31 deletions.
75 changes: 44 additions & 31 deletions pkg/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,43 +24,56 @@ func InitLogger(debug, disable bool) (err error) {
}

func NewLogger(debug, disable bool) (*zap.SugaredLogger, error) {
level := zap.NewAtomicLevel()
if debug {
level.SetLevel(zapcore.DebugLevel)
} else {
level.SetLevel(zapcore.InfoLevel)
}
// First, define our level-handling logic.
errorPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.ErrorLevel
})
logPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
if debug {
return lvl < zapcore.ErrorLevel
}
// Not enable debug level
return zapcore.DebugLevel < lvl && lvl < zapcore.ErrorLevel
})

myConfig := zap.Config{
Level: level,
Encoding: "console",
Development: debug,
DisableStacktrace: true,
DisableCaller: true,
EncoderConfig: zapcore.EncoderConfig{
TimeKey: "Time",
LevelKey: "Level",
NameKey: "Name",
CallerKey: "Caller",
MessageKey: "Msg",
StacktraceKey: "St",
EncodeLevel: zapcore.CapitalColorLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
encoderConfig := zapcore.EncoderConfig{
TimeKey: "Time",
LevelKey: "Level",
NameKey: "Name",
CallerKey: "Caller",
MessageKey: "Msg",
StacktraceKey: "St",
EncodeLevel: zapcore.CapitalColorLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}

consoleEncoder := zapcore.NewConsoleEncoder(encoderConfig)

// High-priority output should also go to standard error, and low-priority
// output should also go to standard out.
consoleLogs := zapcore.Lock(os.Stdout)
consoleErrors := zapcore.Lock(os.Stderr)
if disable {
myConfig.OutputPaths = []string{os.DevNull}
myConfig.ErrorOutputPaths = []string{os.DevNull}
devNull, err := os.Create(os.DevNull)
if err != nil {
return nil, err
}
// Discard low-priority output
consoleLogs = zapcore.Lock(devNull)
}

logger, err := myConfig.Build()
if err != nil {
return nil, xerrors.Errorf("failed to build zap config: %w", err)
core := zapcore.NewTee(
zapcore.NewCore(consoleEncoder, consoleErrors, errorPriority),
zapcore.NewCore(consoleEncoder, consoleLogs, logPriority),
)

opts := []zap.Option{zap.ErrorOutput(zapcore.Lock(os.Stderr))}
if debug {
opts = append(opts, zap.Development())
}
logger := zap.New(core, opts...)

return logger.Sugar(), nil
}
Expand Down

0 comments on commit 9dc1bdf

Please sign in to comment.