Add logging to Query

exp
Jack Christensen 2014-09-03 10:34:45 -05:00
parent b8881af745
commit e47838d926
1 changed files with 17 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package pgx
import (
"errors"
"fmt"
log "gopkg.in/inconshreveable/log15.v2"
"time"
)
@ -46,6 +47,10 @@ type Rows struct {
columnIdx int
err error
closed bool
startTime time.Time
sql string
args []interface{}
logger log.Logger
}
func (rows *Rows) FieldDescriptions() []FieldDescription {
@ -53,12 +58,23 @@ func (rows *Rows) FieldDescriptions() []FieldDescription {
}
func (rows *Rows) close() {
if rows.closed {
return
}
if rows.pool != nil {
rows.pool.Release(rows.conn)
rows.pool = nil
}
rows.closed = true
if rows.err == nil {
endTime := time.Now()
rows.logger.Info("Query", "sql", rows.sql, "args", rows.args, "time", endTime.Sub(rows.startTime))
} else {
rows.logger.Error("Query", "sql", rows.sql, "args", rows.args)
}
}
func (rows *Rows) readUntilReadyForQuery() {
@ -339,7 +355,7 @@ func (rows *Rows) Values() ([]interface{}, error) {
// be returned in an error state. So it is allowed to ignore the error returned
// from Query and handle it in *Rows.
func (c *Conn) Query(sql string, args ...interface{}) (*Rows, error) {
c.rows = Rows{conn: c}
c.rows = Rows{conn: c, startTime: time.Now(), sql: sql, args: args, logger: c.logger}
rows := &c.rows
ps, ok := c.preparedStatements[sql]