Skip to content

Commit

Permalink
Fix bug with issues [17464] fix with zap logger redirection
Browse files Browse the repository at this point in the history
Signed-off-by: Colin <jay_jieliu@outlook.com>
  • Loading branch information
LLiuJJ committed Mar 1, 2024
1 parent e68afe7 commit 4ec87f3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
27 changes: 27 additions & 0 deletions client/pkg/logutil/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package logutil

import (
"os"
"sort"
"time"

Expand All @@ -33,6 +34,32 @@ func CreateDefaultZapLogger(level zapcore.Level) (*zap.Logger, error) {
return c, nil
}

// CreateDefaultZapLogger creates a logger with default zap configuration can redirect log to /dev/null
func CreateUtilZapLogger(level zapcore.Level) *zap.Logger {
infoLevel := zap.LevelEnablerFunc(func(level zapcore.Level) bool {
return level == zapcore.InfoLevel
})
errorFatalLevel := zap.LevelEnablerFunc(func(level zapcore.Level) bool {
return level == zapcore.ErrorLevel || level == zapcore.FatalLevel
})
stdoutSyncer := zapcore.Lock(os.Stdout)
stderrSyncer := zapcore.Lock(os.Stderr)
core := zapcore.NewTee(
zapcore.NewCore(
zapcore.NewJSONEncoder(DefaultZapLoggerConfig.EncoderConfig),
stdoutSyncer,
infoLevel,
),
zapcore.NewCore(
zapcore.NewJSONEncoder(DefaultZapLoggerConfig.EncoderConfig),
stderrSyncer,
errorFatalLevel,
),
)
logger := zap.New(core)
return logger
}

// DefaultZapLoggerConfig defines default zap logger configuration.
var DefaultZapLoggerConfig = zap.Config{
Level: zap.NewAtomicLevelAt(ConvertToZapLevel(DefaultLogLevel)),
Expand Down
5 changes: 1 addition & 4 deletions etcdctl/ctlv3/command/snapshot_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ func snapshotSaveCommandFunc(cmd *cobra.Command, args []string) {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, err)
}

lg, err := logutil.CreateDefaultZapLogger(zap.InfoLevel)
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitError, err)
}
lg := logutil.CreateUtilZapLogger(zap.InfoLevel)
cfg := mustClientCfgFromCmd(cmd)

// if user does not specify "--command-timeout" flag, there will be no timeout for snapshot save command
Expand Down

0 comments on commit 4ec87f3

Please sign in to comment.