-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,9 @@ import ( | |
"errors" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"sync" | ||
) | ||
|
||
var ( | ||
|
@@ -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") | ||
|
||
errLog Logger = log.New(os.Stderr, "[MySQL] ", log.Ldate|log.Ltime|log.Lshortfile) | ||
errLogLock = &sync.Mutex{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the indirection? @xaprb Why should we guard this by a lock at all? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
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) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just pass There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
return driver.ErrBadConn | ||
} | ||
|
||
|
@@ -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 | ||
} | ||
|
||
|
@@ -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 | ||
} | ||
|
||
|
@@ -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 | ||
} | ||
|
||
|
@@ -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 | ||
} | ||
|
||
|
@@ -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 | ||
} | ||
|
||
|
There was a problem hiding this comment.
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 ;)