Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

print lightning log to local file #313

Merged
merged 13 commits into from
May 11, 2020
8 changes: 7 additions & 1 deletion cmd/tidb-lightning/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (

func main() {
cfg := config.Must(config.LoadGlobalConfig(os.Args[1:], nil))
fmt.Fprintf(os.Stdout, "Verbose debug logs will be written to %s.\n\n", cfg.App.Config.File)

app := lightning.New(cfg)

sc := make(chan os.Signal, 1)
Expand Down Expand Up @@ -63,6 +65,7 @@ func main() {
err := app.GoServe()
if err != nil {
logger.Error("failed to start HTTP server", zap.Error(err))
fmt.Fprintln(os.Stderr, "failed to start HTTP server:", err)
return
}

Expand All @@ -72,9 +75,12 @@ func main() {
err = app.RunOnce()
}
if err != nil {
logger.Error("tidb lightning encountered error", zap.Error(err))
logger.Error("tidb lightning encountered error stack info", zap.Error(err))
logger.Error("tidb lightning encountered error", log.ShortError(err))
fmt.Fprintln(os.Stderr, "tidb lightning encountered error: ", err)
} else {
logger.Info("tidb lightning exit")
fmt.Fprintln(os.Stdout, "tidb lightning exit")
}

syncErr := logger.Sync()
Expand Down
8 changes: 7 additions & 1 deletion lightning/config/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"

"github.com/BurntSushi/toml"
"github.com/pingcap/errors"
Expand Down Expand Up @@ -116,6 +118,10 @@ func Must(cfg *GlobalConfig, err error) *GlobalConfig {
return cfg
}

func timestampLogFileName() string {
return filepath.Join(os.TempDir(), time.Now().Format("lightning.log.2006-01-02T15.04.05Z0700"))
}

// LoadGlobalConfig reads the arguments and fills in the GlobalConfig.
func LoadGlobalConfig(args []string, extraFlags func(*flag.FlagSet)) (*GlobalConfig, error) {
cfg := NewGlobalConfig()
Expand All @@ -130,7 +136,7 @@ func LoadGlobalConfig(args []string, extraFlags func(*flag.FlagSet)) (*GlobalCon
printVersion := fs.Bool("V", false, "print version of lightning")

logLevel := fs.String("L", "", `log level: info, debug, warn, error, fatal (default "info")`)
logFilePath := fs.String("log-file", "", "log file path")
logFilePath := fs.String("log-file", timestampLogFileName(), "log file path")
tidbHost := fs.String("tidb-host", "", "TiDB server host")
tidbPort := fs.Int("tidb-port", 0, "TiDB server port (default 4000)")
tidbUser := fs.String("tidb-user", "", "TiDB user name to connect")
Expand Down
3 changes: 2 additions & 1 deletion lightning/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ func (rc *RestoreController) restoreTables(ctx context.Context) error {
tableLogTask := task.tr.logger.Begin(zap.InfoLevel, "restore table")
web.BroadcastTableCheckpoint(task.tr.tableName, task.cp)
err := task.tr.restoreTable(ctx2, rc, task.cp)
err = errors.Annotatef(err, "restore table %s failed", task.tr.tableName)
tableLogTask.End(zap.ErrorLevel, err)
web.BroadcastError(task.tr.tableName, err)
metric.RecordTableCount("completed", err)
Expand Down Expand Up @@ -1246,7 +1247,7 @@ func (rc *RestoreController) cleanCheckpoints(ctx context.Context) error {
err = rc.checkpointsDB.RemoveCheckpoint(ctx, "all")
}
task.End(zap.ErrorLevel, err)
return errors.Trace(err)
return errors.Annotate(err, "clean checkpoints")
}

type chunkRestore struct {
Expand Down