Skip to content

Commit

Permalink
add set logger to support combine log with DM (pingcap#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
lichunzhu authored Mar 18, 2020
1 parent 7996140 commit ffe221d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
13 changes: 3 additions & 10 deletions cmd/dumpling/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ import (

"github.com/pingcap/dumpling/v4/cli"
"github.com/pingcap/dumpling/v4/export"
"github.com/pingcap/dumpling/v4/log"
"github.com/spf13/cobra"
"go.uber.org/zap"
)

var (
Expand Down Expand Up @@ -85,12 +83,6 @@ func init() {
func run() {
println(cli.LongVersion())

err := log.InitAppLogger(&log.Config{Level: logLevel})
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "initialize logger failed: %s", err.Error())
os.Exit(1)
}

conf := export.DefaultConfig()
conf.Database = database
conf.Host = host
Expand All @@ -107,10 +99,11 @@ func run() {
conf.Rows = rows
conf.Where = where
conf.EscapeBackslash = escapeBackslash
conf.LogLevel = logLevel

err = export.Dump(conf)
err := export.Dump(conf)
if err != nil {
log.Zap().Error("dump failed", zap.Error(err))
fmt.Printf("dump failed: %s\n", err.Error())
os.Exit(1)
}
return
Expand Down
4 changes: 4 additions & 0 deletions v4/export/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ type Config struct {
Password string
Threads int

LogLevel string
Logger *zap.Logger

FileSize uint64
StatementSize uint64
OutputDirPath string
Expand All @@ -43,6 +46,7 @@ func DefaultConfig() *Config {
Port: 3306,
Password: "",
Threads: 4,
Logger: nil,
StatusAddr: ":8281",
FileSize: UnspecifiedSize,
StatementSize: UnspecifiedSize,
Expand Down
14 changes: 9 additions & 5 deletions v4/export/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ import (
)

func Dump(conf *Config) (err error) {
if err = adjustConfig(conf); err != nil {
return withStack(err)
}

go func() {
err1 := startDumplingService(conf.StatusAddr)
if err1 != nil {
log.Zap().Error("dumpling stops to serving service", zap.Error(err1))
if conf.StatusAddr != "" {
err1 := startDumplingService(conf.StatusAddr)
if err1 != nil {
log.Zap().Error("dumpling stops to serving service", zap.Error(err1))
}
}
}()
pool, err := sql.Open("mysql", conf.getDSN(""))
Expand All @@ -25,8 +31,6 @@ func Dump(conf *Config) (err error) {
}
defer pool.Close()

adjustConfig(conf)

conf.ServerInfo, err = detectServerInfo(pool)
if err != nil {
return err
Expand Down
14 changes: 13 additions & 1 deletion v4/export/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,23 @@ import (
"github.com/pingcap/dumpling/v4/log"
)

func adjustConfig(conf *Config) {
func adjustConfig(conf *Config) error {
// Init logger
if conf.Logger != nil {
log.SetAppLogger(conf.Logger)
} else {
err := log.InitAppLogger(&log.Config{Level: conf.LogLevel})
if err != nil {
return err
}
}

if conf.Rows != UnspecifiedSize {
// Disable filesize if rows was set
conf.FileSize = UnspecifiedSize
}

return nil
}

func detectServerInfo(db *sql.DB) (ServerInfo, error) {
Expand Down
4 changes: 4 additions & 0 deletions v4/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func InitAppLogger(cfg *Config) error {
return nil
}

func SetAppLogger(logger *zap.Logger) {
appLogger = Logger{logger}
}

func ChangeAppLogLevel(level zapcore.Level) {
appLevel.SetLevel(level)
}

0 comments on commit ffe221d

Please sign in to comment.