Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

*: refline logs #189

Merged
merged 16 commits into from
Mar 20, 2020
15 changes: 13 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)
Expand All @@ -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()
Expand All @@ -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())
Expand All @@ -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 {
Expand Down
19 changes: 18 additions & 1 deletion pkg/summary/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down