From ba35a37b72b61c268a9a8c5784279bede0c1cce4 Mon Sep 17 00:00:00 2001 From: xhe Date: Wed, 16 Nov 2022 14:43:55 +0800 Subject: [PATCH] util: use one logger (#39176) close pingcap/tidb#38941 --- DEPS.bzl | 4 ++-- go.mod | 2 +- go.sum | 4 ++-- util/logutil/log.go | 43 +++++++++++++++++----------------------- util/logutil/log_test.go | 15 -------------- 5 files changed, 23 insertions(+), 45 deletions(-) diff --git a/DEPS.bzl b/DEPS.bzl index 4c3e94cca5a14..1d23df5e1e756 100644 --- a/DEPS.bzl +++ b/DEPS.bzl @@ -2898,8 +2898,8 @@ def go_deps(): name = "com_github_pingcap_log", build_file_proto_mode = "disable_global", importpath = "github.com/pingcap/log", - sum = "h1:T7e5Low0BU2ZazI2dz2mh3W1qv+w8wtvq1YR8DneA0c=", - version = "v1.1.1-0.20221110065318-21a4942860b3", + sum = "h1:crhkw6DD+07Bg1wYhW5Piw+kYNKZqFQqfC2puUf6gMI=", + version = "v1.1.1-0.20221116035753-734d527bc87c", ) go_repository( name = "com_github_pingcap_sysutil", diff --git a/go.mod b/go.mod index 01400b701b85b..c147af6b8506e 100644 --- a/go.mod +++ b/go.mod @@ -69,7 +69,7 @@ require ( github.com/pingcap/failpoint v0.0.0-20220423142525-ae43b7f4e5c3 github.com/pingcap/fn v0.0.0-20200306044125-d5540d389059 github.com/pingcap/kvproto v0.0.0-20221103025916-e7e21f0e9cd9 - github.com/pingcap/log v1.1.1-0.20221110065318-21a4942860b3 + github.com/pingcap/log v1.1.1-0.20221116035753-734d527bc87c github.com/pingcap/sysutil v0.0.0-20220114020952-ea68d2dbf5b4 github.com/pingcap/tidb/parser v0.0.0-20211011031125-9b13dc409c5e github.com/pingcap/tipb v0.0.0-20221020071514-cd933387bcb5 diff --git a/go.sum b/go.sum index 0730b5193bd55..48306f2d8d576 100644 --- a/go.sum +++ b/go.sum @@ -783,8 +783,8 @@ github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9/go.mod h1:4rbK1p9ILyIf github.com/pingcap/log v0.0.0-20200511115504-543df19646ad/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8= github.com/pingcap/log v0.0.0-20210625125904-98ed8e2eb1c7/go.mod h1:8AanEdAHATuRurdGxZXBz0At+9avep+ub7U1AGYLIMM= github.com/pingcap/log v1.1.1-0.20221015072633-39906604fb81/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4= -github.com/pingcap/log v1.1.1-0.20221110065318-21a4942860b3 h1:T7e5Low0BU2ZazI2dz2mh3W1qv+w8wtvq1YR8DneA0c= -github.com/pingcap/log v1.1.1-0.20221110065318-21a4942860b3/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4= +github.com/pingcap/log v1.1.1-0.20221116035753-734d527bc87c h1:crhkw6DD+07Bg1wYhW5Piw+kYNKZqFQqfC2puUf6gMI= +github.com/pingcap/log v1.1.1-0.20221116035753-734d527bc87c/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4= github.com/pingcap/sysutil v0.0.0-20220114020952-ea68d2dbf5b4 h1:HYbcxtnkN3s5tqrZ/z3eJS4j3Db8wMphEm1q10lY/TM= github.com/pingcap/sysutil v0.0.0-20220114020952-ea68d2dbf5b4/go.mod h1:sDCsM39cGiv2vwunZkaFA917vVkqDTGSPbbV7z4Oops= github.com/pingcap/tipb v0.0.0-20221020071514-cd933387bcb5 h1:Yoo8j5xQGxjlsC3yt0ndsiAz0WZXED9rzsKmEN0U0DY= diff --git a/util/logutil/log.go b/util/logutil/log.go index 83eb44eb9d2cc..a7d59ae0c2d0f 100644 --- a/util/logutil/log.go +++ b/util/logutil/log.go @@ -111,37 +111,30 @@ func InitLogger(cfg *LogConfig) error { return errors.Trace(err) } - _, _, err = initGRPCLogger(cfg) - if err != nil { - return errors.Trace(err) - } - + initGRPCLogger(gl) return nil } -func initGRPCLogger(cfg *LogConfig) (*zap.Logger, *log.ZapProperties, error) { - // Copy Config struct by assignment. - config := cfg.Config - var l *zap.Logger - var err error - var prop *log.ZapProperties +func initGRPCLogger(gl *zap.Logger) { + level := zapcore.ErrorLevel + verbosity := 0 if len(os.Getenv("GRPC_DEBUG")) > 0 { - config.Level = "debug" - l, prop, err = log.InitLogger(&config, zap.AddStacktrace(zapcore.FatalLevel)) - if err != nil { - return nil, nil, errors.Trace(err) - } - gzap.ReplaceGrpcLoggerV2WithVerbosity(l, 999) - } else { - config.Level = "error" - l, prop, err = log.InitLogger(&config, zap.AddStacktrace(zapcore.FatalLevel)) - if err != nil { - return nil, nil, errors.Trace(err) - } - gzap.ReplaceGrpcLoggerV2(l) + verbosity = 999 + level = zapcore.DebugLevel } - return l, prop, nil + newgl := gl.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core { + oldcore, ok := core.(*log.TextIOCore) + if !ok { + return oldcore + } + newcore := oldcore.Clone() + leveler := zap.NewAtomicLevel() + leveler.SetLevel(level) + newcore.LevelEnabler = leveler + return newcore + })) + gzap.ReplaceGrpcLoggerV2WithVerbosity(newgl, verbosity) } // ReplaceLogger replace global logger instance with given log config. diff --git a/util/logutil/log_test.go b/util/logutil/log_test.go index d059204973678..57a7786c0e530 100644 --- a/util/logutil/log_test.go +++ b/util/logutil/log_test.go @@ -100,21 +100,6 @@ func TestSetLevel(t *testing.T) { require.Equal(t, zap.DebugLevel, log.GetLevel()) } -func TestGrpcLoggerCreation(t *testing.T) { - level := "info" - conf := NewLogConfig(level, DefaultLogFormat, "", EmptyFileLogConfig, false) - _, p, err := initGRPCLogger(conf) - // assert after init grpc logger, the original conf is not changed - require.Equal(t, conf.Level, level) - require.NoError(t, err) - require.Equal(t, p.Level.Level(), zap.ErrorLevel) - os.Setenv("GRPC_DEBUG", "1") - defer os.Unsetenv("GRPC_DEBUG") - _, newP, err := initGRPCLogger(conf) - require.NoError(t, err) - require.Equal(t, newP.Level.Level(), zap.DebugLevel) -} - func TestSlowQueryLoggerCreation(t *testing.T) { level := "Error" conf := NewLogConfig(level, DefaultLogFormat, "", EmptyFileLogConfig, false)