From 8f806a523447734bf7a799ccd86758531c874a01 Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Thu, 15 Feb 2024 18:27:02 +0100 Subject: [PATCH] Fix grpc log format Signed-off-by: Marek Siarkowicz --- client/v3/logger.go | 2 +- etcdctl/ctlv3/command/global.go | 5 +++-- server/embed/config_logging.go | 8 +++----- server/etcdmain/grpc_proxy.go | 2 +- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/client/v3/logger.go b/client/v3/logger.go index 300363cd25b..49c33b0e6e7 100644 --- a/client/v3/logger.go +++ b/client/v3/logger.go @@ -33,8 +33,8 @@ func init() { if err != nil { panic(err) } + grpclog.SetLoggerV2(zapgrpc.NewLogger(lg.Named("grpc"))) lg = lg.Named("etcd-client") - grpclog.SetLoggerV2(zapgrpc.NewLogger(lg)) } } diff --git a/etcdctl/ctlv3/command/global.go b/etcdctl/ctlv3/command/global.go index 93b62068af6..8dc430881db 100644 --- a/etcdctl/ctlv3/command/global.go +++ b/etcdctl/ctlv3/command/global.go @@ -26,6 +26,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/pflag" "go.uber.org/zap" + "go.uber.org/zap/zapgrpc" "google.golang.org/grpc/grpclog" "go.etcd.io/etcd/client/pkg/v3/logutil" @@ -107,7 +108,6 @@ func clientConfigFromCmd(cmd *cobra.Command) *clientv3.ConfigSpec { cobrautl.ExitWithError(cobrautl.ExitError, err) } if debug { - grpclog.SetLoggerV2(grpclog.NewLoggerV2WithVerbosity(os.Stderr, os.Stderr, os.Stderr, 4)) fs.VisitAll(func(f *pflag.Flag) { fmt.Fprintf(os.Stderr, "%s=%v\n", flags.FlagToEnv("ETCDCTL", f.Name), f.Value) }) @@ -116,8 +116,9 @@ func clientConfigFromCmd(cmd *cobra.Command) *clientv3.ConfigSpec { // too many routine connection disconnects to turn on by default. // // See https://github.com/etcd-io/etcd/pull/9623 for background - grpclog.SetLoggerV2(grpclog.NewLoggerV2(io.Discard, io.Discard, os.Stderr)) + lg = lg.WithOptions(zap.IncreaseLevel(zap.ErrorLevel)) } + grpclog.SetLoggerV2(zapgrpc.NewLogger(lg.Named("grpc"))) cfg := &clientv3.ConfigSpec{} cfg.Endpoints, err = endpointsFromCmd(cmd) diff --git a/server/embed/config_logging.go b/server/embed/config_logging.go index 432b7c89d77..87c066337ec 100644 --- a/server/embed/config_logging.go +++ b/server/embed/config_logging.go @@ -19,9 +19,7 @@ import ( "encoding/json" "errors" "fmt" - "io" "net/url" - "os" "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -227,13 +225,13 @@ func NewZapCoreLoggerBuilder(lg *zap.Logger, _ zapcore.Core, _ zapcore.WriteSync func (cfg *Config) SetupGlobalLoggers() { lg := cfg.GetLogger() if lg != nil { + zap.ReplaceGlobals(lg) if cfg.LogLevel == "debug" { grpc.EnableTracing = true - grpclog.SetLoggerV2(zapgrpc.NewLogger(lg)) } else { - grpclog.SetLoggerV2(grpclog.NewLoggerV2(io.Discard, os.Stderr, os.Stderr)) + lg = lg.WithOptions(zap.IncreaseLevel(zap.WarnLevel)) } - zap.ReplaceGlobals(lg) + grpclog.SetLoggerV2(zapgrpc.NewLogger(lg.Named("grpc"))) } } diff --git a/server/etcdmain/grpc_proxy.go b/server/etcdmain/grpc_proxy.go index 6ba45db7fe2..23b9fed52ff 100644 --- a/server/etcdmain/grpc_proxy.go +++ b/server/etcdmain/grpc_proxy.go @@ -190,7 +190,7 @@ func startGRPCProxy(cmd *cobra.Command, args []string) { } defer lg.Sync() - grpclog.SetLoggerV2(zapgrpc.NewLogger(lg)) + grpclog.SetLoggerV2(zapgrpc.NewLogger(lg.Named("grpc"))) // The proxy itself (ListenCert) can have not-empty CN. // The empty CN is required for grpcProxyCert.