2
0

Add logging on CopyFrom

Logging previously only logged statement results on Exec() and Query(), but not CopyFrom(). This makes change makes it consistent.
This commit is contained in:
Patrick Hemmer
2021-03-07 22:51:32 -05:00
committed by Jack Christensen
parent 0cc35d7a60
commit 41fa6e844c
+14 -1
View File
@@ -5,6 +5,7 @@ import (
"context"
"fmt"
"io"
"time"
"github.com/jackc/pgconn"
"github.com/jackc/pgio"
@@ -145,12 +146,24 @@ func (ct *copyFrom) run(ctx context.Context) (int64, error) {
w.Close()
}()
startTime := time.Now()
commandTag, err := ct.conn.pgConn.CopyFrom(ctx, r, fmt.Sprintf("copy %s ( %s ) from stdin binary;", quotedTableName, quotedColumnNames))
r.Close()
<-doneChan
return commandTag.RowsAffected(), err
rowsAffected := commandTag.RowsAffected()
if err == nil {
if ct.conn.shouldLog(LogLevelInfo) {
endTime := time.Now()
ct.conn.log(ctx, LogLevelInfo, "Copy", map[string]interface{}{"tableName": ct.tableName, "columnNames": ct.columnNames, "time": endTime.Sub(startTime), "rowCount": rowsAffected})
}
} else if ct.conn.shouldLog(LogLevelError) {
ct.conn.log(ctx, LogLevelError, "Copy", map[string]interface{}{"err": err, "tableName": ct.tableName, "columnNames": ct.columnNames})
}
return rowsAffected, err
}
func (ct *copyFrom) buildCopyBuf(buf []byte, sd *pgconn.StatementDescription) (bool, []byte, error) {