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