Optimize current code, fix with more commands

Signed-off-by: Colin <jay_jieliu@outlook.com>
This commit is contained in:
Colin 2024-04-28 22:37:29 +08:00
parent 43f751c504
commit 3c40d60366
4 changed files with 28 additions and 27 deletions

View File

@ -35,36 +35,34 @@ func CreateDefaultZapLogger(level zapcore.Level) (*zap.Logger, error) {
}
// CreateUtilZapLogger creates a logger with default zap configuration can redirect log to /dev/null
func CreateUtilZapLogger(level zapcore.Level) *zap.Logger {
func CreateUtilZapLogger(level zapcore.Level) (*zap.Logger, error) {
lcfg := DefaultZapLoggerConfig
lcfg.Level = zap.NewAtomicLevelAt(level)
lcfg.Development = false
lcfg.Encoding = DefaultLogFormat
lcfg.Sampling = &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
}
infoLevel := zap.LevelEnablerFunc(func(level zapcore.Level) bool {
return level == zapcore.InfoLevel
return level <= zapcore.InfoLevel
})
errorFatalLevel := zap.LevelEnablerFunc(func(level zapcore.Level) bool {
return level == zapcore.ErrorLevel || level == zapcore.FatalLevel
return level > zapcore.InfoLevel
})
stdoutSyncer := zapcore.Lock(os.Stdout)
stderrSyncer := zapcore.Lock(os.Stderr)
core := zapcore.NewTee(
zapcore.NewCore(
zapcore.NewJSONEncoder(lcfg.EncoderConfig),
stdoutSyncer,
infoLevel,
),
zapcore.NewCore(
zapcore.NewJSONEncoder(lcfg.EncoderConfig),
stderrSyncer,
errorFatalLevel,
),
)
return zap.New(core, zap.AddCaller())
opts := []zap.Option{
zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return zapcore.NewTee(
zapcore.NewCore(
zapcore.NewJSONEncoder(lcfg.EncoderConfig),
stdoutSyncer,
infoLevel,
),
zapcore.NewCore(
zapcore.NewJSONEncoder(lcfg.EncoderConfig),
stderrSyncer,
errorFatalLevel,
),
)
}),
}
return lcfg.Build(opts...)
}
// DefaultZapLoggerConfig defines default zap logger configuration.

View File

@ -89,7 +89,7 @@ type epHealth struct {
// epHealthCommandFunc executes the "endpoint-health" command.
func epHealthCommandFunc(cmd *cobra.Command, args []string) {
lg, err := logutil.CreateDefaultZapLogger(zap.InfoLevel)
lg, err := logutil.CreateUtilZapLogger(zap.InfoLevel)
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitError, err)
}
@ -267,7 +267,7 @@ func endpointsFromCluster(cmd *cobra.Command) []string {
cobrautl.ExitWithError(cobrautl.ExitError, err)
}
// exclude auth for not asking needless password (MemberList() doesn't need authentication)
lg, _ := logutil.CreateDefaultZapLogger(zap.InfoLevel)
lg, _ := logutil.CreateUtilZapLogger(zap.InfoLevel)
cfg, err := clientv3.NewClientConfig(&clientv3.ConfigSpec{
Endpoints: eps,
DialTimeout: dt,

View File

@ -89,7 +89,7 @@ func (*discardValue) Set(string) error { return nil }
func (*discardValue) Type() string { return "" }
func clientConfigFromCmd(cmd *cobra.Command) *clientv3.ConfigSpec {
lg, err := logutil.CreateDefaultZapLogger(zap.InfoLevel)
lg, err := logutil.CreateUtilZapLogger(zap.InfoLevel)
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitError, err)
}
@ -152,7 +152,7 @@ func mustClientFromCmd(cmd *cobra.Command) *clientv3.Client {
}
func mustClient(cc *clientv3.ConfigSpec) *clientv3.Client {
lg, _ := logutil.CreateDefaultZapLogger(zap.InfoLevel)
lg, _ := logutil.CreateUtilZapLogger(zap.InfoLevel)
cfg, err := clientv3.NewClientConfig(cc, lg)
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, err)

View File

@ -50,7 +50,10 @@ func snapshotSaveCommandFunc(cmd *cobra.Command, args []string) {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, err)
}
lg := logutil.CreateUtilZapLogger(zap.InfoLevel)
lg, err := logutil.CreateUtilZapLogger(zap.InfoLevel)
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitError, err)
}
cfg := mustClientCfgFromCmd(cmd)
// if user does not specify "--command-timeout" flag, there will be no timeout for snapshot save command