Skip to content

Commit af3a778

Browse files
committed
replace github.com/siddontang/go-log with log/slog
1 parent cf91430 commit af3a778

16 files changed

+54
-108
lines changed

README.md

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ package main
147147

148148
import (
149149
"github.com/go-mysql-org/go-mysql/canal"
150-
"github.com/siddontang/go-log/log"
151150
)
152151

153152
type MyEventHandler struct {
@@ -537,26 +536,7 @@ We pass all tests in https://github.com/bradfitz/go-sql-test using go-mysql driv
537536

538537
## Logging
539538

540-
Logging by default is send to stdout.
541-
542-
To disable logging completely:
543-
```go
544-
import "github.com/siddontang/go-log/log"
545-
...
546-
nullHandler, _ := log.NewNullHandler()
547-
cfg.Logger = log.NewDefault(nullHandler)
548-
```
549-
550-
To write logging to any [`io.Writer`](https://pkg.go.dev/io#Writer):
551-
```go
552-
import "github.com/siddontang/go-log/log"
553-
...
554-
w := ...
555-
streamHandler, _ := log.NewStreamHandler(w)
556-
cfg.Logger = log.NewDefault(streamHandler)
557-
```
558-
559-
Or you can implement your own [`log.Handler`](https://pkg.go.dev/github.com/siddontang/go-log/log#Handler).
539+
Logging uses log/slog.
560540

561541
## How to migrate to this repo
562542
To change the used package in your repo it's enough to add this `replace` directive to your `go.mod`:

canal/canal.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"io"
7+
"log/slog"
78
"net"
89
"os"
910
"regexp"
@@ -21,7 +22,6 @@ import (
2122
"github.com/go-mysql-org/go-mysql/utils"
2223
"github.com/pingcap/errors"
2324
"github.com/pingcap/tidb/pkg/parser"
24-
"github.com/siddontang/go-log/log"
2525
)
2626

2727
// Canal can sync your MySQL data into everywhere, like Elasticsearch, Redis, etc...
@@ -64,8 +64,7 @@ var ErrExcludedTable = errors.New("excluded table meta")
6464
func NewCanal(cfg *Config) (*Canal, error) {
6565
c := new(Canal)
6666
if cfg.Logger == nil {
67-
streamHandler, _ := log.NewStreamHandler(os.Stdout)
68-
cfg.Logger = log.NewDefault(streamHandler)
67+
cfg.Logger = slog.Default()
6968
}
7069
if cfg.Dialer == nil {
7170
dialer := &net.Dialer{}

canal/canal_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"time"
77

88
"github.com/pingcap/tidb/pkg/parser"
9-
"github.com/siddontang/go-log/log"
109
"github.com/stretchr/testify/require"
1110
"github.com/stretchr/testify/suite"
1211

@@ -99,7 +98,7 @@ func (s *canalTestSuite) SetupSuite() {
9998

10099
s.execute("SET GLOBAL binlog_format = 'ROW'")
101100

102-
s.c.SetEventHandler(&testEventHandler{})
101+
s.c.SetEventHandler(&testEventHandler{T: s.T()})
103102
go func() {
104103
set, _ := mysql.ParseGTIDSet("mysql", "")
105104
err = s.c.StartFromGTID(set)
@@ -126,10 +125,11 @@ func (s *canalTestSuite) execute(query string, args ...interface{}) *mysql.Resul
126125

127126
type testEventHandler struct {
128127
DummyEventHandler
128+
T *testing.T
129129
}
130130

131131
func (h *testEventHandler) OnRow(e *RowsEvent) error {
132-
log.Infof("OnRow %s %v\n", e.Action, e.Rows)
132+
h.T.Log("OnRow", e.Action, e.Rows)
133133
umi, ok := e.Rows[0][4].(uint32) // 4th col is umi. mysqldump gives uint64 instead of uint32
134134
if ok && (umi != umiA && umi != umiB && umi != umiC) {
135135
return fmt.Errorf("invalid unsigned medium int %d", umi)

canal/config.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ package canal
22

33
import (
44
"crypto/tls"
5+
"log/slog"
56
"math/rand"
67
"net"
78
"os"
89
"time"
910

1011
"github.com/BurntSushi/toml"
1112
"github.com/pingcap/errors"
12-
"github.com/siddontang/go-log/log"
13-
"github.com/siddontang/go-log/loggers"
1413

1514
"github.com/go-mysql-org/go-mysql/client"
1615
"github.com/go-mysql-org/go-mysql/mysql"
@@ -101,7 +100,7 @@ type Config struct {
101100
TLSConfig *tls.Config
102101

103102
// Set Logger
104-
Logger loggers.Advanced
103+
Logger *slog.Logger
105104

106105
// Set Dialer
107106
Dialer client.Dialer
@@ -150,8 +149,7 @@ func NewDefaultConfig() *Config {
150149
c.Dump.DiscardErr = true
151150
c.Dump.SkipMasterData = false
152151

153-
streamHandler, _ := log.NewStreamHandler(os.Stdout)
154-
c.Logger = log.NewDefault(streamHandler)
152+
c.Logger = slog.Default()
155153

156154
dialer := &net.Dialer{}
157155
c.Dialer = dialer.DialContext

canal/master.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package canal
22

33
import (
4+
"log/slog"
45
"sync"
56

67
"github.com/go-mysql-org/go-mysql/mysql"
7-
"github.com/siddontang/go-log/loggers"
88
)
99

1010
type masterInfo struct {
@@ -16,27 +16,27 @@ type masterInfo struct {
1616

1717
timestamp uint32
1818

19-
logger loggers.Advanced
19+
logger *slog.Logger
2020
}
2121

2222
func (m *masterInfo) Update(pos mysql.Position) {
23-
m.logger.Debugf("update master position %s", pos)
23+
m.logger.Debug("update master position", slog.Any("pos", pos))
2424

2525
m.Lock()
2626
m.pos = pos
2727
m.Unlock()
2828
}
2929

3030
func (m *masterInfo) UpdateTimestamp(ts uint32) {
31-
m.logger.Debugf("update master timestamp %d", ts)
31+
m.logger.Debug("update master timestamp", slog.Int64("ts", int64(ts)))
3232

3333
m.Lock()
3434
m.timestamp = ts
3535
m.Unlock()
3636
}
3737

3838
func (m *masterInfo) UpdateGTIDSet(gset mysql.GTIDSet) {
39-
m.logger.Debugf("update master gtid set %s", gset)
39+
m.logger.Debug("update master gtid set", slog.Any("gset", gset))
4040

4141
m.Lock()
4242
m.gset = gset

client/pool_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"time"
1010

1111
"github.com/go-mysql-org/go-mysql/test_util"
12-
"github.com/siddontang/go-log/log"
1312
"github.com/stretchr/testify/require"
1413
"github.com/stretchr/testify/suite"
1514
)
@@ -30,7 +29,6 @@ func (s *poolTestSuite) TestPool_Close() {
3029
addr := fmt.Sprintf("%s:%s", *test_util.MysqlHost, s.port)
3130
pool, err := NewPoolWithOptions(addr, *testUser, *testPassword, "",
3231
WithPoolLimits(5, 10, 5),
33-
WithLogFunc(log.Debugf),
3432
)
3533
require.NoError(s.T(), err)
3634

@@ -53,7 +51,6 @@ func (s *poolTestSuite) TestPool_WrongPassword() {
5351

5452
_, err := NewPoolWithOptions(addr, *testUser, "wrong-password", "",
5553
WithPoolLimits(5, 10, 5),
56-
WithLogFunc(log.Debugf),
5754
WithNewPoolPingTimeout(time.Second),
5855
)
5956

@@ -71,7 +68,6 @@ func (s *poolTestSuite) TestPool_WrongAddr() {
7168

7269
_, err = NewPoolWithOptions(laddr.String(), *testUser, *testPassword, "",
7370
WithPoolLimits(5, 10, 5),
74-
WithLogFunc(log.Debugf),
7571
WithNewPoolPingTimeout(time.Second),
7672
)
7773

driver/driver_options_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"time"
1717

1818
"github.com/pingcap/errors"
19-
"github.com/siddontang/go-log/log"
2019
"github.com/stretchr/testify/require"
2120

2221
"github.com/go-mysql-org/go-mysql/client"
@@ -40,7 +39,6 @@ type mockHandler struct {
4039
}
4140

4241
func TestDriverOptions_SetRetriesOn(t *testing.T) {
43-
log.SetLevel(log.LevelDebug)
4442
srv := CreateMockServer(t)
4543
defer srv.Stop()
4644
var wg sync.WaitGroup
@@ -66,7 +64,6 @@ func TestDriverOptions_SetRetriesOn(t *testing.T) {
6664
}
6765

6866
func TestDriverOptions_SetRetriesOff(t *testing.T) {
69-
log.SetLevel(log.LevelDebug)
7067
srv := CreateMockServer(t)
7168
defer srv.Stop()
7269
var wg sync.WaitGroup
@@ -117,7 +114,6 @@ func TestDriverOptions_SetCompression(t *testing.T) {
117114
}
118115

119116
func TestDriverOptions_ConnectTimeout(t *testing.T) {
120-
log.SetLevel(log.LevelDebug)
121117
srv := CreateMockServer(t)
122118
defer srv.Stop()
123119

@@ -135,7 +131,6 @@ func TestDriverOptions_ConnectTimeout(t *testing.T) {
135131
}
136132

137133
func TestDriverOptions_BufferSize(t *testing.T) {
138-
log.SetLevel(log.LevelDebug)
139134
srv := CreateMockServer(t)
140135
defer srv.Stop()
141136

@@ -161,7 +156,6 @@ func TestDriverOptions_BufferSize(t *testing.T) {
161156
}
162157

163158
func TestDriverOptions_ReadTimeout(t *testing.T) {
164-
log.SetLevel(log.LevelDebug)
165159
srv := CreateMockServer(t)
166160
defer srv.Stop()
167161

@@ -183,7 +177,6 @@ func TestDriverOptions_ReadTimeout(t *testing.T) {
183177
}
184178

185179
func TestDriverOptions_writeTimeout(t *testing.T) {
186-
log.SetLevel(log.LevelDebug)
187180
srv := CreateMockServer(t)
188181
defer srv.Stop()
189182

@@ -231,7 +224,6 @@ func TestDriverOptions_namedValueChecker(t *testing.T) {
231224
return nil
232225
})
233226

234-
log.SetLevel(log.LevelDebug)
235227
srv := CreateMockServer(t)
236228
defer srv.Stop()
237229
conn, err := sql.Open("mysql", "root@127.0.0.1:3307/test?writeTimeout=1s")

dump/dumper.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"io"
7+
"log/slog"
78
"net"
89
"os"
910
"os/exec"
@@ -12,8 +13,6 @@ import (
1213

1314
"github.com/go-mysql-org/go-mysql/mysql"
1415
"github.com/pingcap/errors"
15-
"github.com/siddontang/go-log/log"
16-
"github.com/siddontang/go-log/loggers"
1716
)
1817

1918
// Unlick mysqldump, Dumper is designed for parsing and syning data easily.
@@ -51,7 +50,7 @@ type Dumper struct {
5150
mysqldumpVersion string
5251
sourceDataSupported bool
5352

54-
Logger loggers.Advanced
53+
Logger *slog.Logger
5554
}
5655

5756
func NewDumper(executionPath string, addr string, user string, password string) (*Dumper, error) {
@@ -96,8 +95,7 @@ func NewDumper(executionPath string, addr string, user string, password string)
9695

9796
d.ErrOut = os.Stderr
9897

99-
streamHandler, _ := log.NewStreamHandler(os.Stdout)
100-
d.Logger = log.NewDefault(streamHandler)
98+
d.Logger = slog.Default()
10199

102100
return d, nil
103101
}
@@ -312,7 +310,7 @@ func (d *Dumper) Dump(w io.Writer) error {
312310
}
313311

314312
args[passwordArgIndex] = "--password=******"
315-
d.Logger.Infof("exec mysqldump with %v", args)
313+
d.Logger.Info("exec mysqldump with", slog.Any("args", args))
316314
args[passwordArgIndex] = passwordArg
317315
cmd := exec.Command(d.ExecutionPath, args...)
318316

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ require (
1515
github.com/pingcap/errors v0.11.5-0.20240311024730-e056997136bb
1616
github.com/pingcap/tidb/pkg/parser v0.0.0-20241118164214-4f047be191be
1717
github.com/shopspring/decimal v1.2.0
18-
github.com/siddontang/go-log v0.0.0-20180807004314-8d05993dda07
1918
github.com/stretchr/testify v1.8.4
2019
)
2120

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR
4848
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
4949
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
5050
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
51-
github.com/siddontang/go-log v0.0.0-20180807004314-8d05993dda07 h1:oI+RNwuC9jF2g2lP0u0cVEEZrc/AYBCuFdvwrLWM/6Q=
52-
github.com/siddontang/go-log v0.0.0-20180807004314-8d05993dda07/go.mod h1:yFdBgwXP24JziuRl2NMUahT7nGLNOKi1SIiFxMttVD4=
5351
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5452
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
5553
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=

mysql/mariadb_gtid.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package mysql
33
import (
44
"bytes"
55
"fmt"
6+
"log/slog"
67
"sort"
78
"strconv"
89
"strings"
910

1011
"github.com/pingcap/errors"
11-
"github.com/siddontang/go-log/log"
1212
)
1313

1414
// MariadbGTID represent mariadb gtid, [domain ID]-[server-id]-[sequence]
@@ -93,7 +93,7 @@ func (gtid *MariadbGTID) forward(newer *MariadbGTID) error {
9393
| mysqld-bin.000001 | 2215 | Gtid | 111 | 2257 | BEGIN GTID 0-111-6 |
9494
*/
9595
if newer.SequenceNumber <= gtid.SequenceNumber {
96-
log.Warnf("out of order binlog appears with gtid %s vs current position gtid %s", newer, gtid)
96+
slog.Warn("out of order binlog", slog.Any("new", newer), slog.Any("current", gtid))
9797
}
9898

9999
gtid.ServerID = newer.ServerID

mysql/queryattributes.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package mysql
22

33
import (
44
"encoding/binary"
5-
6-
"github.com/siddontang/go-log/log"
5+
"fmt"
6+
"log/slog"
77
)
88

99
// Query Attributes in MySQL are key/value pairs passed along with COM_QUERY or COM_STMT_EXECUTE
@@ -27,7 +27,7 @@ func (qa *QueryAttribute) TypeAndFlag() []byte {
2727
case uint64:
2828
return []byte{MYSQL_TYPE_LONGLONG, PARAM_UNSIGNED}
2929
default:
30-
log.Warnf("query attribute with unsupported type %T", v)
30+
slog.Warn("query attribute with unsupported type", slog.String("type", fmt.Sprintf("%T", v)))
3131
}
3232
return []byte{0x0, 0x0} // type 0x0, flag 0x0, to not break the protocol
3333
}
@@ -42,7 +42,7 @@ func (qa *QueryAttribute) ValueBytes() []byte {
4242
binary.LittleEndian.PutUint64(b, v)
4343
return b
4444
default:
45-
log.Warnf("query attribute with unsupported type %T", v)
45+
slog.Warn("query attribute with unsupported type", slog.String("type", fmt.Sprintf("%T", v)))
4646
}
4747
return []byte{0x0} // 0 length value to not break the protocol
4848
}

replication/binlogstreamer.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"time"
66

77
"github.com/pingcap/errors"
8-
"github.com/siddontang/go-log/log"
98
)
109

1110
var (
@@ -74,8 +73,6 @@ func (s *BinlogStreamer) close() {
7473
func (s *BinlogStreamer) closeWithError(err error) {
7574
if err == nil {
7675
err = ErrSyncClosed
77-
} else {
78-
log.Errorf("close sync with err: %v", err)
7976
}
8077

8178
select {

0 commit comments

Comments
 (0)