Skip to content
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

fix log doesn't show useful filename&lineno #1588

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,29 @@ var (
errBadConnNoWrite = errors.New("bad connection")
)

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

// Logger is used to log critical error messages.
type Logger interface {
Print(v ...any)
}

// stdLog wraps log.Logger to adjust calldepth.
type stdLog struct {
l *log.Logger
}

func (s *stdLog) Print(v ...any) {
// log.Logger.Print() uses a calldepth of 2.
// We need to skip one more frame for mysqlConn.log().
_ = s.l.Output(3, fmt.Sprint(v...))
}

// StdLogger creates a Logger that wraps a log.Logger.
func StdLogger(l *log.Logger) Logger {
return &stdLog{l}
}

// NopLogger is a nop implementation of the Logger interface.
type NopLogger struct{}

Expand Down