Skip to content

log: add "filename:line" prefix by ourself #1589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"io"
"net"
"runtime"
"strconv"
"strings"
"sync/atomic"
Expand Down Expand Up @@ -47,7 +49,17 @@

// 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 (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.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, 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-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.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.0)

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.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.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, 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-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-11.1)

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, 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, 5.7)

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, 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, 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, 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.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
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 (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.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, 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-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.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.0)

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.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.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, 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-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-11.1)

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, 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, 5.7)

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, 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, 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, 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.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
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 (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.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, 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-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.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.0)

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.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.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, 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-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-11.1)

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, 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, 5.7)

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, 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, 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, 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.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
}
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 (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.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, 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-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.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.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 (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.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.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, 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-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-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 (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, 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, 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 (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, 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, 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, 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.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
}

// 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
Loading