Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util/logutil: Remove useless grpc log in production (#25454) #27239

Merged
merged 3 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions util/logutil/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ const (
DefaultRecordPlanInSlowLog = 1
// DefaultTiDBEnableSlowLog enables TiDB to log slow queries.
DefaultTiDBEnableSlowLog = true
// GRPCLogDebugVerbosity enables max verbosity when debugging grpc code.
GRPCLogDebugVerbosity = 99
)

// EmptyFileLogConfig is an empty FileLogConfig.
Expand Down Expand Up @@ -106,20 +104,42 @@ func InitLogger(cfg *LogConfig) error {
log.ReplaceGlobals(gl, props)

// init dedicated logger for slow query log
SlowQueryLogger, err = newSlowQueryLogger(cfg)
SlowQueryLogger, _, err = newSlowQueryLogger(cfg)
if err != nil {
return errors.Trace(err)
}

// init logger for grpc debugging
_, _, err = initGRPCLogger(cfg)
if err != nil {
return errors.Trace(err)
}

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
if len(os.Getenv("GRPC_DEBUG")) > 0 {
// more information for verbosity: https://github.com/google/glog#verbose-logging
gzap.ReplaceGrpcLoggerV2WithVerbosity(gl, GRPCLogDebugVerbosity)
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 {
gzap.ReplaceGrpcLoggerV2(gl)
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)
}

return nil
return l, prop, nil
}

// SetLevel sets the zap logger's level.
Expand Down
25 changes: 25 additions & 0 deletions util/logutil/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,28 @@ func TestSetLevel(t *testing.T) {
require.NoError(t, err)
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.Nil(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.Nil(t, err)
require.Equal(t, newP.Level.Level(), zap.DebugLevel)
}

func TestSlowQueryLoggerCreation(t *testing.T) {
level := "warn"
conf := NewLogConfig(level, DefaultLogFormat, "", EmptyFileLogConfig, false)
_, prop, err := newSlowQueryLogger(conf)
// assert after init slow query logger, the original conf is not changed
require.Equal(t, conf.Level, level)
require.Nil(t, err)
require.Equal(t, prop.Level.String(), conf.Level)
}
16 changes: 9 additions & 7 deletions util/logutil/slow_query_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (

var _pool = buffer.NewPool()

func newSlowQueryLogger(cfg *LogConfig) (*zap.Logger, error) {
func newSlowQueryLogger(cfg *LogConfig) (*zap.Logger, *log.ZapProperties, error) {

// reuse global config and override slow query log file
// copy global config and override slow query log file
// if slow query log filename is empty, slow query log will behave the same as global log
sqConfig := &cfg.Config
sqConfig := cfg.Config
if len(cfg.SlowQueryFile) != 0 {
sqConfig.File = log.FileLogConfig{
MaxSize: cfg.File.MaxSize,
Expand All @@ -26,17 +26,19 @@ func newSlowQueryLogger(cfg *LogConfig) (*zap.Logger, error) {
}

// create the slow query logger
sqLogger, prop, err := log.InitLogger(sqConfig)
sqLogger, prop, err := log.InitLogger(&sqConfig)
if err != nil {
return nil, errors.Trace(err)
return nil, nil, errors.Trace(err)
}

// replace 2018-12-19-unified-log-format text encoder with slow log encoder
newCore := log.NewTextCore(&slowLogEncoder{}, prop.Syncer, prop.Level)
sqLogger = sqLogger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return log.NewTextCore(&slowLogEncoder{}, prop.Syncer, prop.Level)
return newCore
}))
prop.Core = newCore

return sqLogger, nil
return sqLogger, prop, nil
}

type slowLogEncoder struct{}
Expand Down