From f7d1de25fa042a4bd7eb5c03309ade4173856d5d Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Thu, 31 Mar 2022 20:23:45 +0200 Subject: [PATCH] server: Use default logging configuration instead of zap production one This fixes problem where logs json changes format of timestamp. --- CHANGELOG/CHANGELOG-3.5.md | 1 + CHANGELOG/CHANGELOG-3.6.md | 1 + client/pkg/logutil/zap.go | 11 ++++ client/v3/client.go | 6 +- client/v3/logger.go | 22 +------ client/v3/snapshot/v3_snapshot.go | 2 +- etcdctl/ctlv3/command/ep_command.go | 5 +- etcdctl/ctlv3/command/global.go | 7 +- etcdctl/ctlv3/command/snapshot_command.go | 3 +- etcdutl/etcdutl/common.go | 3 +- etcdutl/snapshot/v3_snapshot.go | 2 +- pkg/netutil/netutil.go | 5 +- pkg/proxy/server.go | 11 +--- server/etcdmain/config.go | 3 +- server/etcdmain/etcd.go | 12 ++-- server/etcdmain/gateway.go | 4 +- server/etcdmain/grpc_proxy.go | 10 ++- server/etcdmain/main.go | 2 +- tests/e2e/zap_logging_test.go | 78 +++++++++++++++++++++++ tests/framework/e2e/cluster.go | 5 ++ 20 files changed, 134 insertions(+), 59 deletions(-) create mode 100644 tests/e2e/zap_logging_test.go diff --git a/CHANGELOG/CHANGELOG-3.5.md b/CHANGELOG/CHANGELOG-3.5.md index 6bf16109d38e..4e8477ad76f1 100644 --- a/CHANGELOG/CHANGELOG-3.5.md +++ b/CHANGELOG/CHANGELOG-3.5.md @@ -11,6 +11,7 @@ The minimum recommended etcd versions to run in **production** are 3.3.18+, 3.4. ### etcd server - Fix [Provide a better liveness probe for when etcd runs as a Kubernetes pod](https://github.com/etcd-io/etcd/pull/13706) +- Fix [inconsistent log format](https://github.com/etcd-io/etcd/pull/13864) ### package `client/pkg/v3` diff --git a/CHANGELOG/CHANGELOG-3.6.md b/CHANGELOG/CHANGELOG-3.6.md index 9210d427f7ca..383cf82e7f1d 100644 --- a/CHANGELOG/CHANGELOG-3.6.md +++ b/CHANGELOG/CHANGELOG-3.6.md @@ -63,6 +63,7 @@ See [code changes](https://github.com/etcd-io/etcd/compare/v3.5.0...v3.6.0). - Fix [A client can cause a nil dereference in etcd by passing an invalid SortTarget](https://github.com/etcd-io/etcd/pull/13555) - Fix [Grant lease with negative ID can possibly cause db out of sync](https://github.com/etcd-io/etcd/pull/13676) - Fix [segmentation violation(SIGSEGV) error due to premature unlocking of watchableStore](https://github.com/etcd-io/etcd/pull/13505) +- Fix [inconsistent log format](https://github.com/etcd-io/etcd/pull/13864) ### tools/benchmark diff --git a/client/pkg/logutil/zap.go b/client/pkg/logutil/zap.go index 33d95e9cb177..0a4374c77b8d 100644 --- a/client/pkg/logutil/zap.go +++ b/client/pkg/logutil/zap.go @@ -21,6 +21,17 @@ import ( "go.uber.org/zap/zapcore" ) +// CreateDefaultZapLogger creates a logger with default zap configuration +func CreateDefaultZapLogger(level zapcore.Level) (*zap.Logger, error) { + lcfg := DefaultZapLoggerConfig + lcfg.Level = zap.NewAtomicLevelAt(level) + c, err := lcfg.Build() + if err != nil { + return nil, err + } + return c, nil +} + // DefaultZapLoggerConfig defines default zap logger configuration. var DefaultZapLoggerConfig = zap.Config{ Level: zap.NewAtomicLevelAt(ConvertToZapLevel(DefaultLogLevel)), diff --git a/client/v3/client.go b/client/v3/client.go index 971fea607cf9..91c091adfc91 100644 --- a/client/v3/client.go +++ b/client/v3/client.go @@ -24,6 +24,7 @@ import ( "time" "go.etcd.io/etcd/api/v3/v3rpc/rpctypes" + "go.etcd.io/etcd/client/pkg/v3/logutil" "go.etcd.io/etcd/client/v3/credentials" "go.etcd.io/etcd/client/v3/internal/endpoint" "go.etcd.io/etcd/client/v3/internal/resolver" @@ -370,7 +371,10 @@ func newClient(cfg *Config) (*Client, error) { } else if cfg.LogConfig != nil { client.lg, err = cfg.LogConfig.Build() } else { - client.lg, err = CreateDefaultZapLogger() + client.lg, err = logutil.CreateDefaultZapLogger(etcdClientDebugLevel()) + if client.lg != nil { + client.lg = client.lg.Named("etcd-client") + } } if err != nil { return nil, err diff --git a/client/v3/logger.go b/client/v3/logger.go index 71a9e161ce84..ecac42730f61 100644 --- a/client/v3/logger.go +++ b/client/v3/logger.go @@ -19,7 +19,6 @@ import ( "os" "go.etcd.io/etcd/client/pkg/v3/logutil" - "go.uber.org/zap" "go.uber.org/zap/zapcore" "go.uber.org/zap/zapgrpc" "google.golang.org/grpc/grpclog" @@ -29,10 +28,11 @@ func init() { // We override grpc logger only when the environment variable is set // in order to not interfere by default with user's code or other libraries. if os.Getenv("ETCD_CLIENT_DEBUG") != "" { - lg, err := CreateDefaultZapLogger() + lg, err := logutil.CreateDefaultZapLogger(etcdClientDebugLevel()) if err != nil { panic(err) } + lg = lg.Named("etcd-client") grpclog.SetLoggerV2(zapgrpc.NewLogger(lg)) } } @@ -57,21 +57,3 @@ func etcdClientDebugLevel() zapcore.Level { } return l } - -// CreateDefaultZapLoggerConfig creates a logger config that is configurable using env variable: -// ETCD_CLIENT_DEBUG= debug|info|warn|error|dpanic|panic|fatal|true (true=info) -func CreateDefaultZapLoggerConfig() zap.Config { - lcfg := logutil.DefaultZapLoggerConfig - lcfg.Level = zap.NewAtomicLevelAt(etcdClientDebugLevel()) - return lcfg -} - -// CreateDefaultZapLogger creates a logger that is configurable using env variable: -// ETCD_CLIENT_DEBUG= debug|info|warn|error|dpanic|panic|fatal|true (true=info) -func CreateDefaultZapLogger() (*zap.Logger, error) { - c, err := CreateDefaultZapLoggerConfig().Build() - if err != nil { - return nil, err - } - return c.Named("etcd-client"), nil -} diff --git a/client/v3/snapshot/v3_snapshot.go b/client/v3/snapshot/v3_snapshot.go index d5a8c2682888..8c2f7b37cbdc 100644 --- a/client/v3/snapshot/v3_snapshot.go +++ b/client/v3/snapshot/v3_snapshot.go @@ -46,7 +46,7 @@ func hasChecksum(n int64) bool { // Etcd