Skip to content

Commit 40d9024

Browse files
committed
Make logger configurable per connection
1 parent d83ecdc commit 40d9024

12 files changed

+50
-32
lines changed

Diff for: AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ Stan Putrya <root.vagner at gmail.com>
9595
Stanley Gunawan <gunawan.stanley at gmail.com>
9696
Steven Hartland <steven.hartland at multiplay.co.uk>
9797
Tan Jinhua <312841925 at qq.com>
98+
Tetsuro Aoki <t.aoki1130 at gmail.com>
9899
Thomas Wodarek <wodarekwebpage at gmail.com>
99100
Tim Ruffles <timruffles at gmail.com>
100101
Tom Jenkinson <tom at tjenkinson.me>

Diff for: auth.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ func (mc *mysqlConn) auth(authData []byte, plugin string) ([]byte, error) {
291291
return enc, err
292292

293293
default:
294-
errLog.Print("unknown auth plugin:", plugin)
294+
mc.errLog().Print("unknown auth plugin:", plugin)
295295
return nil, ErrUnknownPlugin
296296
}
297297
}

Diff for: connection.go

+15-8
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (mc *mysqlConn) Begin() (driver.Tx, error) {
105105

106106
func (mc *mysqlConn) begin(readOnly bool) (driver.Tx, error) {
107107
if mc.closed.Load() {
108-
errLog.Print(ErrInvalidConn)
108+
mc.errLog().Print(ErrInvalidConn)
109109
return nil, driver.ErrBadConn
110110
}
111111
var q string
@@ -147,7 +147,7 @@ func (mc *mysqlConn) cleanup() {
147147
return
148148
}
149149
if err := mc.netConn.Close(); err != nil {
150-
errLog.Print(err)
150+
mc.errLog().Print(err)
151151
}
152152
}
153153

@@ -163,14 +163,14 @@ func (mc *mysqlConn) error() error {
163163

164164
func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
165165
if mc.closed.Load() {
166-
errLog.Print(ErrInvalidConn)
166+
mc.errLog().Print(ErrInvalidConn)
167167
return nil, driver.ErrBadConn
168168
}
169169
// Send command
170170
err := mc.writeCommandPacketStr(comStmtPrepare, query)
171171
if err != nil {
172172
// STMT_PREPARE is safe to retry. So we can return ErrBadConn here.
173-
errLog.Print(err)
173+
mc.errLog().Print(err)
174174
return nil, driver.ErrBadConn
175175
}
176176

@@ -204,7 +204,7 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin
204204
buf, err := mc.buf.takeCompleteBuffer()
205205
if err != nil {
206206
// can not take the buffer. Something must be wrong with the connection
207-
errLog.Print(err)
207+
mc.errLog().Print(err)
208208
return "", ErrInvalidConn
209209
}
210210
buf = buf[:0]
@@ -296,7 +296,7 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin
296296

297297
func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, error) {
298298
if mc.closed.Load() {
299-
errLog.Print(ErrInvalidConn)
299+
mc.errLog().Print(ErrInvalidConn)
300300
return nil, driver.ErrBadConn
301301
}
302302
if len(args) != 0 {
@@ -357,7 +357,7 @@ func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, erro
357357

358358
func (mc *mysqlConn) query(query string, args []driver.Value) (*textRows, error) {
359359
if mc.closed.Load() {
360-
errLog.Print(ErrInvalidConn)
360+
mc.errLog().Print(ErrInvalidConn)
361361
return nil, driver.ErrBadConn
362362
}
363363
if len(args) != 0 {
@@ -451,7 +451,7 @@ func (mc *mysqlConn) finish() {
451451
// Ping implements driver.Pinger interface
452452
func (mc *mysqlConn) Ping(ctx context.Context) (err error) {
453453
if mc.closed.Load() {
454-
errLog.Print(ErrInvalidConn)
454+
mc.errLog().Print(ErrInvalidConn)
455455
return driver.ErrBadConn
456456
}
457457

@@ -648,3 +648,10 @@ func (mc *mysqlConn) ResetSession(ctx context.Context) error {
648648
func (mc *mysqlConn) IsValid() bool {
649649
return !mc.closed.Load()
650650
}
651+
652+
func (mc *mysqlConn) errLog() Logger {
653+
if mc.cfg.Logger != nil {
654+
return mc.cfg.Logger
655+
}
656+
return defaultLogger
657+
}

Diff for: connection_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ func TestPingErrInvalidConn(t *testing.T) {
179179
buf: newBuffer(nc),
180180
maxAllowedPacket: defaultMaxAllowedPacket,
181181
closech: make(chan struct{}),
182+
cfg: NewConfig(),
182183
}
183184

184185
err := ms.Ping(context.Background())

Diff for: connector.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
9292
authResp, err := mc.auth(authData, plugin)
9393
if err != nil {
9494
// try the default auth plugin, if using the requested plugin failed
95-
errLog.Print("could not use requested auth plugin '"+plugin+"': ", err.Error())
95+
c.errLog().Print("could not use requested auth plugin '"+plugin+"': ", err.Error())
9696
plugin = defaultAuthPlugin
9797
authResp, err = mc.auth(authData, plugin)
9898
if err != nil {
@@ -144,3 +144,10 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
144144
func (c *connector) Driver() driver.Driver {
145145
return &MySQLDriver{}
146146
}
147+
148+
func (c *connector) errLog() Logger {
149+
if c.cfg.Logger != nil {
150+
return c.cfg.Logger
151+
}
152+
return defaultLogger
153+
}

Diff for: driver_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1995,7 +1995,7 @@ func TestInsertRetrieveEscapedData(t *testing.T) {
19951995
func TestUnixSocketAuthFail(t *testing.T) {
19961996
runTests(t, dsn, func(dbt *DBTest) {
19971997
// Save the current logger so we can restore it.
1998-
oldLogger := errLog
1998+
oldLogger := defaultLogger
19991999

20002000
// Set a new logger so we can capture its output.
20012001
buffer := bytes.NewBuffer(make([]byte, 0, 64))

Diff for: dsn.go

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type Config struct {
5050
Timeout time.Duration // Dial timeout
5151
ReadTimeout time.Duration // I/O read timeout
5252
WriteTimeout time.Duration // I/O write timeout
53+
Logger Logger // Logger
5354

5455
AllowAllFiles bool // Allow all files to be used with LOAD DATA LOCAL INFILE
5556
AllowCleartextPasswords bool // Allows the cleartext client side plugin

Diff for: errors.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,20 @@ var (
3737
errBadConnNoWrite = errors.New("bad connection")
3838
)
3939

40-
var errLog = Logger(log.New(os.Stderr, "[mysql] ", log.Ldate|log.Ltime|log.Lshortfile))
40+
var defaultLogger = Logger(log.New(os.Stderr, "[mysql] ", log.Ldate|log.Ltime|log.Lshortfile))
4141

4242
// Logger is used to log critical error messages.
4343
type Logger interface {
4444
Print(v ...interface{})
4545
}
4646

47-
// SetLogger is used to set the logger for critical errors.
47+
// SetLogger is used to set the default logger for critical errors.
4848
// The initial logger is os.Stderr.
4949
func SetLogger(logger Logger) error {
5050
if logger == nil {
5151
return errors.New("logger is nil")
5252
}
53-
errLog = logger
53+
defaultLogger = logger
5454
return nil
5555
}
5656

Diff for: errors_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import (
1616
)
1717

1818
func TestErrorsSetLogger(t *testing.T) {
19-
previous := errLog
19+
previous := defaultLogger
2020
defer func() {
21-
errLog = previous
21+
defaultLogger = previous
2222
}()
2323

2424
// set up logger
@@ -28,7 +28,7 @@ func TestErrorsSetLogger(t *testing.T) {
2828

2929
// print
3030
SetLogger(logger)
31-
errLog.Print("test")
31+
defaultLogger.Print("test")
3232

3333
// check result
3434
if actual := buffer.String(); actual != expected {

Diff for: packets.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
3434
if cerr := mc.canceled.Value(); cerr != nil {
3535
return nil, cerr
3636
}
37-
errLog.Print(err)
37+
mc.errLog().Print(err)
3838
mc.Close()
3939
return nil, ErrInvalidConn
4040
}
@@ -56,7 +56,7 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
5656
if pktLen == 0 {
5757
// there was no previous packet
5858
if prevData == nil {
59-
errLog.Print(ErrMalformPkt)
59+
mc.errLog().Print(ErrMalformPkt)
6060
mc.Close()
6161
return nil, ErrInvalidConn
6262
}
@@ -70,7 +70,7 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
7070
if cerr := mc.canceled.Value(); cerr != nil {
7171
return nil, cerr
7272
}
73-
errLog.Print(err)
73+
mc.errLog().Print(err)
7474
mc.Close()
7575
return nil, ErrInvalidConn
7676
}
@@ -119,7 +119,7 @@ func (mc *mysqlConn) writePacket(data []byte) error {
119119
}
120120
}
121121
if err != nil {
122-
errLog.Print("closing bad idle connection: ", err)
122+
mc.errLog().Print("closing bad idle connection: ", err)
123123
mc.Close()
124124
return driver.ErrBadConn
125125
}
@@ -161,7 +161,7 @@ func (mc *mysqlConn) writePacket(data []byte) error {
161161
// Handle error
162162
if err == nil { // n != len(data)
163163
mc.cleanup()
164-
errLog.Print(ErrMalformPkt)
164+
mc.errLog().Print(ErrMalformPkt)
165165
} else {
166166
if cerr := mc.canceled.Value(); cerr != nil {
167167
return cerr
@@ -171,7 +171,7 @@ func (mc *mysqlConn) writePacket(data []byte) error {
171171
return errBadConnNoWrite
172172
}
173173
mc.cleanup()
174-
errLog.Print(err)
174+
mc.errLog().Print(err)
175175
}
176176
return ErrInvalidConn
177177
}
@@ -322,7 +322,7 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, plugin string
322322
data, err := mc.buf.takeSmallBuffer(pktLen + 4)
323323
if err != nil {
324324
// cannot take the buffer. Something must be wrong with the connection
325-
errLog.Print(err)
325+
mc.errLog().Print(err)
326326
return errBadConnNoWrite
327327
}
328328

@@ -404,7 +404,7 @@ func (mc *mysqlConn) writeAuthSwitchPacket(authData []byte) error {
404404
data, err := mc.buf.takeSmallBuffer(pktLen)
405405
if err != nil {
406406
// cannot take the buffer. Something must be wrong with the connection
407-
errLog.Print(err)
407+
mc.errLog().Print(err)
408408
return errBadConnNoWrite
409409
}
410410

@@ -424,7 +424,7 @@ func (mc *mysqlConn) writeCommandPacket(command byte) error {
424424
data, err := mc.buf.takeSmallBuffer(4 + 1)
425425
if err != nil {
426426
// cannot take the buffer. Something must be wrong with the connection
427-
errLog.Print(err)
427+
mc.errLog().Print(err)
428428
return errBadConnNoWrite
429429
}
430430

@@ -443,7 +443,7 @@ func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error {
443443
data, err := mc.buf.takeBuffer(pktLen + 4)
444444
if err != nil {
445445
// cannot take the buffer. Something must be wrong with the connection
446-
errLog.Print(err)
446+
mc.errLog().Print(err)
447447
return errBadConnNoWrite
448448
}
449449

@@ -464,7 +464,7 @@ func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error {
464464
data, err := mc.buf.takeSmallBuffer(4 + 1 + 4)
465465
if err != nil {
466466
// cannot take the buffer. Something must be wrong with the connection
467-
errLog.Print(err)
467+
mc.errLog().Print(err)
468468
return errBadConnNoWrite
469469
}
470470

@@ -938,7 +938,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
938938
}
939939
if err != nil {
940940
// cannot take the buffer. Something must be wrong with the connection
941-
errLog.Print(err)
941+
mc.errLog().Print(err)
942942
return errBadConnNoWrite
943943
}
944944

@@ -1137,7 +1137,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
11371137
if valuesCap != cap(paramValues) {
11381138
data = append(data[:pos], paramValues...)
11391139
if err = mc.buf.store(data); err != nil {
1140-
errLog.Print(err)
1140+
mc.errLog().Print(err)
11411141
return errBadConnNoWrite
11421142
}
11431143
}

Diff for: packets_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ func TestReadPacketFail(t *testing.T) {
265265
mc := &mysqlConn{
266266
buf: newBuffer(conn),
267267
closech: make(chan struct{}),
268+
cfg: NewConfig(),
268269
}
269270

270271
// illegal empty (stand-alone) packet

Diff for: statement.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (stmt *mysqlStmt) CheckNamedValue(nv *driver.NamedValue) (err error) {
5151

5252
func (stmt *mysqlStmt) Exec(args []driver.Value) (driver.Result, error) {
5353
if stmt.mc.closed.Load() {
54-
errLog.Print(ErrInvalidConn)
54+
stmt.mc.errLog().Print(ErrInvalidConn)
5555
return nil, driver.ErrBadConn
5656
}
5757
// Send command
@@ -99,7 +99,7 @@ func (stmt *mysqlStmt) Query(args []driver.Value) (driver.Rows, error) {
9999

100100
func (stmt *mysqlStmt) query(args []driver.Value) (*binaryRows, error) {
101101
if stmt.mc.closed.Load() {
102-
errLog.Print(ErrInvalidConn)
102+
stmt.mc.errLog().Print(ErrInvalidConn)
103103
return nil, driver.ErrBadConn
104104
}
105105
// Send command

0 commit comments

Comments
 (0)