Merge de12c6651702f032af63acd6c33e5bf5fabda18c into c86c93ca2951338115159dcdd20711603044e1f1

This commit is contained in:
Colin 2024-09-26 22:00:14 +01:00 committed by GitHub
commit 384b6a706a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 38 additions and 6 deletions

View File

@ -15,6 +15,7 @@
package logutil
import (
"os"
"sort"
"time"
@ -33,6 +34,37 @@ func CreateDefaultZapLogger(level zapcore.Level) (*zap.Logger, error) {
return c, nil
}
// CreateUtilZapLogger creates a logger with default zap configuration can redirect log to /dev/null
func CreateUtilZapLogger(level zapcore.Level) (*zap.Logger, error) {
lcfg := DefaultZapLoggerConfig
lcfg.Level = zap.NewAtomicLevelAt(level)
infoLevel := zap.LevelEnablerFunc(func(level zapcore.Level) bool {
return level <= zapcore.InfoLevel
})
errorFatalLevel := zap.LevelEnablerFunc(func(level zapcore.Level) bool {
return level > zapcore.InfoLevel
})
stdoutSyncer := zapcore.Lock(os.Stdout)
stderrSyncer := zapcore.Lock(os.Stderr)
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.
var DefaultZapLoggerConfig = zap.Config{
Level: zap.NewAtomicLevelAt(ConvertToZapLevel(DefaultLogLevel)),

View File

@ -90,7 +90,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)
}
@ -268,7 +268,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)
}
@ -138,7 +138,7 @@ func clientConfigFromCmd(cmd *cobra.Command) *clientv3.ConfigSpec {
func mustClientCfgFromCmd(cmd *cobra.Command) *clientv3.Config {
cc := clientConfigFromCmd(cmd)
lg, _ := logutil.CreateDefaultZapLogger(zap.InfoLevel)
lg, _ := logutil.CreateUtilZapLogger(zap.InfoLevel)
cfg, err := clientv3.NewClientConfig(cc, lg)
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, 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

@ -74,7 +74,7 @@ func snapshotSaveCommandFunc(cmd *cobra.Command, args []string) {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, err)
}
lg, err := logutil.CreateDefaultZapLogger(zap.InfoLevel)
lg, err := logutil.CreateUtilZapLogger(zap.InfoLevel)
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitError, err)
}