Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

canal,dump: Improve logging #953

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This repo uses [Changelog](CHANGELOG.md).
* [Client](#client)
* [Fake server](#server)
* [database/sql like driver](#driver)
* [Logging](#logging)

## Replication

Expand Down Expand Up @@ -492,6 +493,29 @@ func main() {

We pass all tests in https://github.com/bradfitz/go-sql-test using go-mysql driver. :-)

## Logging

Logging by default is send to stdout.

To disable logging completely:
```go
import "github.com/siddontang/go-log/log"
...
nullHandler, _ := log.NewNullHandler()
cfg.Logger = log.NewDefault(nullHandler)
```

To write logging to any [`io.Writer`](https://pkg.go.dev/io#Writer):
```go
import "github.com/siddontang/go-log/log"
...
w := ...
streamHandler, _ := log.NewStreamHandler(w)
cfg.Logger = log.NewDefault(streamHandler)
```

Or you can implement your own [`log.Handler`](https://pkg.go.dev/github.com/siddontang/go-log/log#Handler).

## Donate

If you like the project and want to buy me a cola, you can through:
Expand Down
3 changes: 3 additions & 0 deletions canal/canal.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ func (c *Canal) prepareDumper() error {
return nil
}

// use the same logger for the dumper
c.dumper.Logger = c.cfg.Logger

dbs := c.cfg.Dump.Databases
tables := c.cfg.Dump.Tables
tableDB := c.cfg.Dump.TableDB
Expand Down
8 changes: 7 additions & 1 deletion dump/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
. "github.com/go-mysql-org/go-mysql/mysql"
"github.com/pingcap/errors"
"github.com/siddontang/go-log/log"
"github.com/siddontang/go-log/loggers"
)

// Unlick mysqldump, Dumper is designed for parsing and syning data easily.
Expand Down Expand Up @@ -49,6 +50,8 @@ type Dumper struct {

mysqldumpVersion string
sourceDataSupported bool

Logger loggers.Advanced
}

func NewDumper(executionPath string, addr string, user string, password string) (*Dumper, error) {
Expand Down Expand Up @@ -93,6 +96,9 @@ func NewDumper(executionPath string, addr string, user string, password string)

d.ErrOut = os.Stderr

streamHandler, _ := log.NewStreamHandler(os.Stdout)
d.Logger = log.NewDefault(streamHandler)

return d, nil
}

Expand Down Expand Up @@ -306,7 +312,7 @@ func (d *Dumper) Dump(w io.Writer) error {
}

args[passwordArgIndex] = "--password=******"
log.Infof("exec mysqldump with %v", args)
d.Logger.Infof("exec mysqldump with %v", args)
args[passwordArgIndex] = passwordArg
cmd := exec.Command(d.ExecutionPath, args...)

Expand Down
Loading