Skip to content

Commit 961210b

Browse files
committed
log: add "filename:line" prefix by ourself
go-sql-driver#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.
1 parent af8d793 commit 961210b

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

connection.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import (
1313
"database/sql"
1414
"database/sql/driver"
1515
"encoding/json"
16+
"fmt"
1617
"io"
1718
"net"
19+
"runtime"
1820
"strconv"
1921
"strings"
2022
"sync/atomic"
@@ -47,7 +49,17 @@ type mysqlConn struct {
4749

4850
// Helper function to call per-connection logger.
4951
func (mc *mysqlConn) log(v ...any) {
50-
mc.cfg.Logger.Print(v...)
52+
_, filename, lineno, ok := runtime.Caller(1)
53+
prefix := ""
54+
if ok {
55+
pos = strings.LastIndexByte(filename, '/')
56+
if pos != -1 {
57+
filename = filename[pos+1:]
58+
}
59+
prefix = fmt.Sprintf("%s:%d: ", filename, lineno)
60+
}
61+
62+
mc.cfg.Logger.Print(prefix, v...)
5163
}
5264

5365
// Handles parameters set in DSN after the connection is established

errors.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var (
3737
errBadConnNoWrite = errors.New("bad connection")
3838
)
3939

40-
var defaultLogger = Logger(log.New(os.Stderr, "[mysql] ", log.Ldate|log.Ltime|log.Lshortfile))
40+
var defaultLogger = Logger(log.New(os.Stderr, "[mysql] ", log.Ldate|log.Ltime))
4141

4242
// Logger is used to log critical error messages.
4343
type Logger interface {

0 commit comments

Comments
 (0)