Skip to content

Commit

Permalink
util: use one logger (#39176) (#39186)
Browse files Browse the repository at this point in the history
close #38941
  • Loading branch information
ti-chi-bot authored Nov 16, 2022
1 parent fc66f32 commit b2c62c6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 43 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,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-20220517085838-12e2f5a9d167
github.com/pingcap/log v1.1.0
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-20220314125451-bfb5c2c55188
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,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 v0.0.0-20211215031037-e024ba4eb0ee/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4=
github.com/pingcap/log v1.1.0 h1:ELiPxACz7vdo1qAvvaWJg1NrYFoY6gqAh/+Uo6aXdD8=
github.com/pingcap/log v1.1.0/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-20220314125451-bfb5c2c55188 h1:+46isFI9fR9R+nJVDMI55tCC/TCwp+bvVA4HLGEv1rY=
Expand Down
43 changes: 18 additions & 25 deletions util/logutil/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 0 additions & 15 deletions util/logutil/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b2c62c6

Please sign in to comment.