Skip to content

Commit 28484dd

Browse files
committed
Merge pull request #182 from arnehormann/configurable-logger
add a configurable logger
2 parents 05994a9 + 35ee96f commit 28484dd

File tree

5 files changed

+68
-16
lines changed

5 files changed

+68
-16
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Version 1.2 (pending)
2+
3+
New Features:
4+
5+
- Logging of critical errors is configurable with `SetLogger`
6+
17
## Version 1.1 (2013-11-02)
28

39
Changes:

errors.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"errors"
1414
"fmt"
1515
"io"
16+
"log"
17+
"os"
1618
)
1719

1820
var (
@@ -24,8 +26,26 @@ var (
2426
errPktSync = errors.New("Commands out of sync. You can't run this command now")
2527
errPktSyncMul = errors.New("Commands out of sync. Did you run multiple statements at once?")
2628
errPktTooLarge = errors.New("Packet for query is too large. You can change this value on the server by adjusting the 'max_allowed_packet' variable.")
29+
errBusyBuffer = errors.New("Busy buffer")
30+
31+
errLog Logger = log.New(os.Stderr, "[MySQL] ", log.Ldate|log.Ltime|log.Lshortfile)
2732
)
2833

34+
// Logger is used to log critical error messages.
35+
type Logger interface {
36+
Print(v ...interface{})
37+
}
38+
39+
// SetLogger is used to set the logger for critical errors.
40+
// The initial logger is stderr.
41+
func SetLogger(logger Logger) error {
42+
if logger == nil {
43+
return errors.New("logger is nil")
44+
}
45+
errLog = logger
46+
return nil
47+
}
48+
2949
// MySQLError is an error type which represents a single MySQL error
3050
type MySQLError struct {
3151
Number uint16

errors_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2+
//
3+
// Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
4+
//
5+
// This Source Code Form is subject to the terms of the Mozilla Public
6+
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
7+
// You can obtain one at http://mozilla.org/MPL/2.0/.
8+
9+
package mysql
10+
11+
import (
12+
"bytes"
13+
"log"
14+
"testing"
15+
)
16+
17+
func TestSetLogger(t *testing.T) {
18+
previous := errLog
19+
defer func() {
20+
errLog = previous
21+
}()
22+
const expected = "prefix: test\n"
23+
buffer := bytes.NewBuffer(make([]byte, 0, 64))
24+
logger := log.New(buffer, "prefix: ", 0)
25+
SetLogger(logger)
26+
errLog.Print("test")
27+
if actual := buffer.String(); actual != expected {
28+
t.Errorf("expected %q, got %q", expected, actual)
29+
}
30+
}

packets.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
2929
// Read packet header
3030
data, err := mc.buf.readNext(4)
3131
if err != nil {
32-
errLog.Print(err.Error())
32+
errLog.Print(err)
3333
mc.Close()
3434
return nil, driver.ErrBadConn
3535
}
@@ -38,7 +38,7 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
3838
pktLen := int(uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16)
3939

4040
if pktLen < 1 {
41-
errLog.Print(errMalformPkt.Error())
41+
errLog.Print(errMalformPkt)
4242
mc.Close()
4343
return nil, driver.ErrBadConn
4444
}
@@ -56,7 +56,7 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
5656
// Read packet body [pktLen bytes]
5757
data, err = mc.buf.readNext(pktLen)
5858
if err != nil {
59-
errLog.Print(err.Error())
59+
errLog.Print(err)
6060
mc.Close()
6161
return nil, driver.ErrBadConn
6262
}
@@ -113,9 +113,9 @@ func (mc *mysqlConn) writePacket(data []byte) error {
113113

114114
// Handle error
115115
if err == nil { // n != len(data)
116-
errLog.Print(errMalformPkt.Error())
116+
errLog.Print(errMalformPkt)
117117
} else {
118-
errLog.Print(err.Error())
118+
errLog.Print(err)
119119
}
120120
return driver.ErrBadConn
121121
}
@@ -228,7 +228,7 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
228228
data := mc.buf.takeSmallBuffer(pktLen + 4)
229229
if data == nil {
230230
// can not take the buffer. Something must be wrong with the connection
231-
errLog.Print("Busy buffer")
231+
errLog.Print(errBusyBuffer)
232232
return driver.ErrBadConn
233233
}
234234

@@ -299,7 +299,7 @@ func (mc *mysqlConn) writeOldAuthPacket(cipher []byte) error {
299299
data := mc.buf.takeSmallBuffer(pktLen + 4)
300300
if data == nil {
301301
// can not take the buffer. Something must be wrong with the connection
302-
errLog.Print("Busy buffer")
302+
errLog.Print(errBusyBuffer)
303303
return driver.ErrBadConn
304304
}
305305

@@ -320,7 +320,7 @@ func (mc *mysqlConn) writeCommandPacket(command byte) error {
320320
data := mc.buf.takeSmallBuffer(4 + 1)
321321
if data == nil {
322322
// can not take the buffer. Something must be wrong with the connection
323-
errLog.Print("Busy buffer")
323+
errLog.Print(errBusyBuffer)
324324
return driver.ErrBadConn
325325
}
326326

@@ -339,7 +339,7 @@ func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error {
339339
data := mc.buf.takeBuffer(pktLen + 4)
340340
if data == nil {
341341
// can not take the buffer. Something must be wrong with the connection
342-
errLog.Print("Busy buffer")
342+
errLog.Print(errBusyBuffer)
343343
return driver.ErrBadConn
344344
}
345345

@@ -360,7 +360,7 @@ func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error {
360360
data := mc.buf.takeSmallBuffer(4 + 1 + 4)
361361
if data == nil {
362362
// can not take the buffer. Something must be wrong with the connection
363-
errLog.Print("Busy buffer")
363+
errLog.Print(errBusyBuffer)
364364
return driver.ErrBadConn
365365
}
366366

@@ -751,7 +751,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
751751
}
752752
if data == nil {
753753
// can not take the buffer. Something must be wrong with the connection
754-
errLog.Print("Busy buffer")
754+
errLog.Print(errBusyBuffer)
755755
return driver.ErrBadConn
756756
}
757757

utils.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,19 @@ import (
1616
"errors"
1717
"fmt"
1818
"io"
19-
"log"
2019
"net/url"
21-
"os"
2220
"strings"
2321
"time"
2422
)
2523

2624
var (
27-
errLog *log.Logger // Error Logger
2825
tlsConfigRegister map[string]*tls.Config // Register for custom tls.Configs
2926

3027
errInvalidDSNUnescaped = errors.New("Invalid DSN: Did you forget to escape a param value?")
3128
errInvalidDSNAddr = errors.New("Invalid DSN: Network Address not terminated (missing closing brace)")
3229
)
3330

3431
func init() {
35-
errLog = log.New(os.Stderr, "[MySQL] ", log.Ldate|log.Ltime|log.Lshortfile)
3632
tlsConfigRegister = make(map[string]*tls.Config)
3733
}
3834

@@ -678,5 +674,5 @@ func appendLengthEncodedInteger(b []byte, n uint64) []byte {
678674
return append(b, 0xfd, byte(n), byte(n>>8), byte(n>>16))
679675
}
680676
return append(b, 0xfe, byte(n), byte(n>>8), byte(n>>16), byte(n>>24),
681-
byte(n>>32), byte(n>>40), byte(n>>48), byte(n>>56))
677+
byte(n>>32), byte(n>>40), byte(n>>48), byte(n>>56))
682678
}

0 commit comments

Comments
 (0)