From 961210b4246ad87f896c40c8a798671453696bef Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Sun, 19 May 2024 12:09:11 +0900 Subject: [PATCH 1/2] log: add "filename:line" prefix by ourself go-sql-driver/mysql#1563 broke the filename:lineno prefix in the log message by introducing a helper function. This commit adds the "filename:line" prefix in the helper function instead of log.Lshortfile option to show correct filename:lineno. --- connection.go | 14 +++++++++++++- errors.go | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/connection.go b/connection.go index 7b8abeb00..1e76df802 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,7 +49,17 @@ type mysqlConn struct { // Helper function to call per-connection logger. func (mc *mysqlConn) log(v ...any) { - mc.cfg.Logger.Print(v...) + _, filename, lineno, ok := runtime.Caller(1) + prefix := "" + if ok { + pos = strings.LastIndexByte(filename, '/') + if pos != -1 { + filename = filename[pos+1:] + } + prefix = fmt.Sprintf("%s:%d: ", filename, lineno) + } + + mc.cfg.Logger.Print(prefix, v...) } // Handles parameters set in DSN after the connection is established diff --git a/errors.go b/errors.go index a7ef88909..238e480f3 100644 --- a/errors.go +++ b/errors.go @@ -37,7 +37,7 @@ var ( errBadConnNoWrite = errors.New("bad connection") ) -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 { From 80e2d8f975a827281c12737d63af7a00335ac499 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Sun, 19 May 2024 12:34:14 +0900 Subject: [PATCH 2/2] fixup --- connection.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/connection.go b/connection.go index 1e76df802..a3dc09d2c 100644 --- a/connection.go +++ b/connection.go @@ -50,16 +50,16 @@ type mysqlConn struct { // Helper function to call per-connection logger. func (mc *mysqlConn) log(v ...any) { _, filename, lineno, ok := runtime.Caller(1) - prefix := "" if ok { - pos = strings.LastIndexByte(filename, '/') + pos := strings.LastIndexByte(filename, '/') if pos != -1 { filename = filename[pos+1:] } - prefix = fmt.Sprintf("%s:%d: ", filename, lineno) + prefix := fmt.Sprintf("%s:%d ", filename, lineno) + v = append([]any{prefix}, v...) } - mc.cfg.Logger.Print(prefix, v...) + mc.cfg.Logger.Print(v...) } // Handles parameters set in DSN after the connection is established