From 672748233b118e5f2851a5bf98fb479b94c0d87b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Fri, 22 Nov 2024 10:56:02 +0100 Subject: [PATCH] canal,dump: Improve logging --- README.md | 24 ++++++++++++++++++++++++ canal/canal.go | 3 +++ dump/dumper.go | 8 +++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ba1813a0b..a768a3747 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ This repo uses [Changelog](CHANGELOG.md). * [Client](#client) * [Fake server](#server) * [database/sql like driver](#driver) +* [Logging](#logging) ## Replication @@ -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: diff --git a/canal/canal.go b/canal/canal.go index 64a1c343e..005c67050 100644 --- a/canal/canal.go +++ b/canal/canal.go @@ -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 diff --git a/dump/dumper.go b/dump/dumper.go index b2b7e4377..91179a0c1 100644 --- a/dump/dumper.go +++ b/dump/dumper.go @@ -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. @@ -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) { @@ -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 } @@ -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...)