diff --git a/connection.go b/connection.go index 42a873406..602c60d72 100644 --- a/connection.go +++ b/connection.go @@ -13,8 +13,10 @@ import ( "database/sql" "database/sql/driver" "encoding/json" + "fmt" "io" "net" + "runtime" "strconv" "strings" "sync/atomic" @@ -47,6 +49,16 @@ type mysqlConn struct { // Helper function to call per-connection logger. func (mc *mysqlConn) log(v ...any) { + _, filename, lineno, ok := runtime.Caller(1) + if ok { + pos := strings.LastIndexByte(filename, '/') + if pos != -1 { + filename = filename[pos+1:] + } + prefix := fmt.Sprintf("%s:%d ", filename, lineno) + v = append([]any{prefix}, v...) + } + mc.cfg.Logger.Print(v...) } @@ -209,7 +221,6 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin mc.cleanup() // interpolateParams would be called before sending any query. // So its safe to retry. - mc.log(err) return "", driver.ErrBadConn } buf = buf[:0] diff --git a/driver_test.go b/driver_test.go index 4fd196d4b..24d73c34f 100644 --- a/driver_test.go +++ b/driver_test.go @@ -3539,6 +3539,9 @@ func TestConnectionAttributes(t *testing.T) { } func TestErrorInMultiResult(t *testing.T) { + if !available { + t.Skipf("MySQL server not running on %s", netAddr) + } // https://github.com/go-sql-driver/mysql/issues/1361 var db *sql.DB if _, err := ParseDSN(dsn); err != errInvalidDSNUnsafeCollation { diff --git a/errors.go b/errors.go index 8c1c75391..dcd389050 100644 --- a/errors.go +++ b/errors.go @@ -31,7 +31,7 @@ var ( ErrBusyBuffer = errors.New("busy buffer") ) -var defaultLogger = Logger(log.New(os.Stderr, "[mysql] ", log.Ldate|log.Ltime|log.Lshortfile)) +var defaultLogger = Logger(log.New(os.Stderr, "[mysql] ", log.Ldate|log.Ltime)) // Logger is used to log critical error messages. type Logger interface { diff --git a/packets.go b/packets.go index ec62bbe38..72d0b72ab 100644 --- a/packets.go +++ b/packets.go @@ -117,8 +117,8 @@ func (mc *mysqlConn) writePacket(data []byte) error { // Write packet if mc.writeTimeout > 0 { if err := mc.netConn.SetWriteDeadline(time.Now().Add(mc.writeTimeout)); err != nil { - mc.log(err) mc.cleanup() + mc.log(err) return err } }