diff --git a/cmd/cmd.go b/cmd/cmd.go index 5b2801894..3e6abff67 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -6,8 +6,11 @@ import ( "context" "net/http" "net/http/pprof" + "os" + "path/filepath" "sync" "sync/atomic" + "time" "github.com/pingcap/log" "github.com/pingcap/tidb/util/logutil" @@ -16,6 +19,7 @@ import ( "go.uber.org/zap" "github.com/pingcap/br/pkg/gluetidb" + "github.com/pingcap/br/pkg/summary" "github.com/pingcap/br/pkg/task" "github.com/pingcap/br/pkg/utils" ) @@ -41,6 +45,10 @@ const ( flagVersionShort = "V" ) +func timestampLogFileName() string { + return filepath.Join(os.TempDir(), "br-"+time.Now().Format(time.RFC3339)) +} + // AddFlags adds flags to the given cmd. func AddFlags(cmd *cobra.Command) { cmd.Version = utils.BRInfo() @@ -49,8 +57,8 @@ func AddFlags(cmd *cobra.Command) { cmd.PersistentFlags().StringP(FlagLogLevel, "L", "info", "Set the log level") - cmd.PersistentFlags().String(FlagLogFile, "", - "Set the log file path. If not set, logs will output to stdout") + cmd.PersistentFlags().String(FlagLogFile, timestampLogFileName(), + "Set the log file path. If not set, logs will output to temp file") cmd.PersistentFlags().String(FlagStatusAddr, "", "Set the HTTP listening address for the status report service. Set to empty string to disable") task.DefineCommonFlags(cmd.PersistentFlags()) @@ -75,6 +83,9 @@ func Init(cmd *cobra.Command) (err error) { } if len(conf.File.Filename) != 0 { atomic.StoreUint64(&hasLogFile, 1) + summary.InitCollector(true) + } else { + cmd.Printf("log file: %s\n", conf.File.Filename) } lg, p, e := log.InitLogger(conf) if e != nil { diff --git a/pkg/summary/collector.go b/pkg/summary/collector.go index ee465d60b..e1d722a18 100644 --- a/pkg/summary/collector.go +++ b/pkg/summary/collector.go @@ -40,7 +40,24 @@ type LogCollector interface { type logFunc func(msg string, fields ...zap.Field) -var collector = newLogCollector(log.Info) +var collector LogCollector = newLogCollector(log.Info) + +// InitCollector initilize global collector instance. +func InitCollector(hasLogFile bool) { + logF := log.L().Info + if hasLogFile { + conf := new(log.Config) + // Always duplicate summary to stdout. + logger, _, err := log.InitLogger(conf) + if err == nil { + logF = func(msg string, fields ...zap.Field) { + logger.Info(msg, fields...) + log.Info(msg, fields...) + } + } + } + collector = newLogCollector(logF) +} type logCollector struct { mu sync.Mutex