diff --git a/query.go b/query.go index 2be37797..8ef8655e 100644 --- a/query.go +++ b/query.go @@ -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]