Skip to content

Commit

Permalink
log: add "filename:line" prefix by ourself
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
methane committed May 19, 2024
1 parent af8d793 commit 961210b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"io"
"net"
"runtime"
"strconv"
"strings"
"sync/atomic"
Expand Down Expand Up @@ -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, '/')

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / lint

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / lint

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, 8.0)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.20, 8.0)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, mariadb-10.11)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, 5.7)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, 8.3)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21, 8.0)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, 8.3)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, mariadb-10.11)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, mariadb-10.5)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, 8.0)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, mariadb-10.6)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, mariadb-11.3)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, mariadb-11.1)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, 5.7)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, mariadb-10.6)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, mariadb-10.5)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, mariadb-11.3)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, 5.7)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, mariadb-11.1)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, mariadb-10.6)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, mariadb-10.11)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, 8.0)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, 8.3)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, mariadb-10.5)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, mariadb-11.3)

undefined: pos

Check failure on line 55 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, mariadb-11.1)

undefined: pos
if pos != -1 {

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / lint

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / lint

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, 8.0)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.20, 8.0)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, mariadb-10.11)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, 5.7)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, 8.3)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21, 8.0)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, 8.3)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, mariadb-10.11)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, mariadb-10.5)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, 8.0)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, mariadb-10.6)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, mariadb-11.3)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, mariadb-11.1)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, 5.7)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, mariadb-10.6)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, mariadb-10.5)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, mariadb-11.3)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, 5.7)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, mariadb-11.1)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, mariadb-10.6)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, mariadb-10.11)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, 8.0)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, 8.3)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, mariadb-10.5)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, mariadb-11.3)

undefined: pos

Check failure on line 56 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, mariadb-11.1)

undefined: pos
filename = filename[pos+1:]

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / lint

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / lint

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, 8.0)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.20, 8.0)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, mariadb-10.11)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, 5.7)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, 8.3)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21, 8.0)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, 8.3)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, mariadb-10.11)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, mariadb-10.5)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, 8.0)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, mariadb-10.6)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, mariadb-11.3)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, mariadb-11.1)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, 5.7)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, mariadb-10.6)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, mariadb-10.5)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, mariadb-11.3)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, 5.7)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, mariadb-11.1)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, mariadb-10.6)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, mariadb-10.11)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, 8.0)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, 8.3)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, mariadb-10.5)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, mariadb-11.3)

undefined: pos

Check failure on line 57 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, mariadb-11.1)

undefined: pos
}
prefix = fmt.Sprintf("%s:%d: ", filename, lineno)
}

mc.cfg.Logger.Print(prefix, v...)

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / lint

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / lint

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, 8.0)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.20, 8.0)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, mariadb-10.11)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, 5.7)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, 8.3)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21, 8.0)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, 8.3)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, mariadb-10.11)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, mariadb-10.5)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, 8.0)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, mariadb-10.6)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, mariadb-11.3)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, mariadb-11.1)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22, 5.7)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, mariadb-10.6)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, mariadb-10.5)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, mariadb-11.3)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, 5.7)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.22, mariadb-11.1)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, mariadb-10.6)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, mariadb-10.11)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, 8.0)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, 8.3)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, mariadb-10.5)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, mariadb-11.3)

too many arguments in call to mc.cfg.Logger.Print

Check failure on line 62 in connection.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.22, mariadb-11.1)

too many arguments in call to mc.cfg.Logger.Print
}

// Handles parameters set in DSN after the connection is established
Expand Down
2 changes: 1 addition & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 961210b

Please sign in to comment.