Skip to content
Closed
Changes from all commits
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
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