From 59497ded6f6487502171448254dcb343f4dc50f4 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Sat, 18 May 2024 23:59:40 +0900 Subject: [PATCH] fix log doesn't show useful filename&lineno --- errors.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/errors.go b/errors.go index a7ef88909..b2907fd5a 100644 --- a/errors.go +++ b/errors.go @@ -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{}