Skip to content

Commit

Permalink
use deepcopy to make init more safer
Browse files Browse the repository at this point in the history
  • Loading branch information
SabaPing committed Jun 15, 2021
1 parent c1cc79a commit 6c76acc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
25 changes: 24 additions & 1 deletion util/logutil/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func InitLogger(cfg *LogConfig) error {

func initGRPCLogger(cfg *LogConfig) error {

config := &cfg.Config
config := deepcopyLogConfig(&cfg.Config)
// hack: force stdout
config.File.Filename = ""
if len(os.Getenv("GRPC_DEBUG")) > 0 {
Expand All @@ -141,6 +141,29 @@ func initGRPCLogger(cfg *LogConfig) error {
return nil
}

func deepcopyLogConfig(o *log.Config) *log.Config {
return &log.Config{
Level: o.Level,
Format: o.Format,
DisableTimestamp: o.DisableTimestamp,
File: log.FileLogConfig{
Filename: o.File.Filename,
MaxSize: o.File.MaxSize,
MaxDays: o.File.MaxDays,
MaxBackups: o.File.MaxBackups,
},
Development: o.Development,
DisableCaller: o.DisableCaller,
DisableStacktrace: o.DisableStacktrace,
DisableErrorVerbose: o.DisableErrorVerbose,
Sampling: &zap.SamplingConfig{
Initial: o.Sampling.Initial,
Thereafter: o.Sampling.Thereafter,
Hook: o.Sampling.Hook,
},
}
}

// InitZapLogger is delegated to InitLogger.
// Deprecated: prefer InitLogger
func InitZapLogger(cfg *LogConfig) error {
Expand Down
2 changes: 1 addition & 1 deletion util/logutil/slow_query_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func newSlowQueryLogger(cfg *LogConfig) (*zap.Logger, error) {

// reuse 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 := deepcopyLogConfig(&cfg.Config)
if len(cfg.SlowQueryFile) != 0 {
sqConfig.File = log.FileLogConfig{
MaxSize: cfg.File.MaxSize,
Expand Down

0 comments on commit 6c76acc

Please sign in to comment.