Skip to content

add a configurable logger #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 4, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
"errors"
"fmt"
"io"
"log"
"os"
"sync"
)

var (
Expand All @@ -24,8 +27,30 @@ var (
errPktSync = errors.New("Commands out of sync. You can't run this command now")
errPktSyncMul = errors.New("Commands out of sync. Did you run multiple statements at once?")
errPktTooLarge = errors.New("Packet for query is too large. You can change this value on the server by adjusting the 'max_allowed_packet' variable.")
errBusyBuffer = errors.New("Busy buffer")
errNoLogger = errors.New("logger is nil")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This err is only used once and not exported. Keep it inline ;)


errLog Logger = log.New(os.Stderr, "[MySQL] ", log.Ldate|log.Ltime|log.Lshortfile)
errLogLock = &sync.Mutex{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the indirection?
I guess just errLogLock sync.Mutex should be enough.

@xaprb Why should we guard this by a lock at all?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uhm... true. The Mutex is only needed if the caller has to be prevented from sabotaging the driver - to his or her own detriment.
I'll drop it.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only suggested the lock for an overabundance of caution.

)

// Logger is used to log critical error messages.
type Logger interface {
Print(v ...interface{})
}

// SetLogger is used to set the logger for critical errors.
// The initial logger is stderr.
func SetLogger(logger Logger) error {
if logger == nil {
return errNoLogger
}
errLogLock.Lock()
errLog = logger
errLogLock.Unlock()
return nil
}

// MySQLError is an error type which represents a single MySQL error
type MySQLError struct {
Number uint16
Expand Down
30 changes: 30 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
//
// Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.

package mysql

import (
"bytes"
"log"
"testing"
)

func TestSetLogger(t *testing.T) {
previous := errLog
defer func() {
errLog = previous
}()
const expected = "prefix: test\n"
buffer := bytes.NewBuffer(make([]byte, 0, 64))
logger := log.New(buffer, "prefix: ", 0)
SetLogger(logger)
errLog.Print("test")
if actual := buffer.String(); actual != expected {
t.Errorf("expected %q, got %q", expected, actual)
}
}
12 changes: 6 additions & 6 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
data := mc.buf.takeSmallBuffer(pktLen + 4)
if data == nil {
// can not take the buffer. Something must be wrong with the connection
errLog.Print("Busy buffer")
errLog.Print(errBusyBuffer.Error())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just pass errBusyBuffer, this makes it a bit easier to check for errors in a custom logger implementation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I follwed the pattern used here: https://github.com/go-sql-driver/mysql/blob/master/packets.go#L41
I'll change that too, then.

return driver.ErrBadConn
}

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

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

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

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

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

Expand Down
6 changes: 1 addition & 5 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,19 @@ import (
"errors"
"fmt"
"io"
"log"
"net/url"
"os"
"strings"
"time"
)

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

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

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

Expand Down Expand Up @@ -678,5 +674,5 @@ func appendLengthEncodedInteger(b []byte, n uint64) []byte {
return append(b, 0xfd, byte(n), byte(n>>8), byte(n>>16))
}
return append(b, 0xfe, byte(n), byte(n>>8), byte(n>>16), byte(n>>24),
byte(n>>32), byte(n>>40), byte(n>>48), byte(n>>56))
byte(n>>32), byte(n>>40), byte(n>>48), byte(n>>56))
}