Skip to content

Commit

Permalink
canal,dump: Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dveeden committed Nov 22, 2024
1 parent 4a647d0 commit b037cb2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
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

0 comments on commit b037cb2

Please sign in to comment.