Skip to content

Commit 2f7015e

Browse files
authored
log: add "filename:line" prefix by ourself (#1589)
#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 2f7015e

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

connection.go

+12
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,6 +49,16 @@ type mysqlConn struct {
4749

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

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)