Skip to content

Commit

Permalink
fix log doesn't show useful filename&lineno
Browse files Browse the repository at this point in the history
  • Loading branch information
methane committed May 18, 2024
1 parent af8d793 commit 59497de
Showing 1 changed file with 17 additions and 1 deletion.
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

0 comments on commit 59497de

Please sign in to comment.