Skip to content

Commit

Permalink
log: add "filename:line" prefix by ourself (go-sql-driver#1589)
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 Jun 4, 2024
1 parent af8d793 commit 2f7015e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions 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,6 +49,16 @@ type mysqlConn struct {

// Helper function to call per-connection logger.
func (mc *mysqlConn) log(v ...any) {
_, filename, lineno, ok := runtime.Caller(1)
if ok {
pos := strings.LastIndexByte(filename, '/')
if pos != -1 {
filename = filename[pos+1:]
}
prefix := fmt.Sprintf("%s:%d ", filename, lineno)
v = append([]any{prefix}, v...)
}

mc.cfg.Logger.Print(v...)
}

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 2f7015e

Please sign in to comment.