mirror of
https://github.com/jackc/pgx.git
synced 2025-05-31 11:42:24 +00:00
Improve logging
This commit is contained in:
parent
cbddbb423e
commit
ad88123f9c
34
conn.go
34
conn.go
@ -140,17 +140,17 @@ func Connect(config ConnConfig) (c *Conn, err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
c.logger.Debug("Using default User " + user.Username)
|
|
||||||
c.config.User = user.Username
|
c.config.User = user.Username
|
||||||
|
c.logger.Debug("Using default connection config", "User", c.config.User)
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.config.Port == 0 {
|
if c.config.Port == 0 {
|
||||||
c.logger.Debug("Using default Port")
|
|
||||||
c.config.Port = 5432
|
c.config.Port = 5432
|
||||||
|
c.logger.Debug("Using default connection config", "Port", c.config.Port)
|
||||||
}
|
}
|
||||||
if c.config.MsgBufSize == 0 {
|
if c.config.MsgBufSize == 0 {
|
||||||
c.logger.Debug("Using default MsgBufSize")
|
|
||||||
c.config.MsgBufSize = 1024
|
c.config.MsgBufSize = 1024
|
||||||
|
c.logger.Debug("Using default connection config", "MsgBufSize", c.config.MsgBufSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.config.Socket != "" {
|
if c.config.Socket != "" {
|
||||||
@ -279,13 +279,20 @@ func ParseURI(uri string) (ConnConfig, error) {
|
|||||||
// need to simultaneously store the entire result set in memory. It also means that
|
// need to simultaneously store the entire result set in memory. It also means that
|
||||||
// it is possible to process some rows and then for an error to occur. Callers
|
// it is possible to process some rows and then for an error to occur. Callers
|
||||||
// should be aware of this possibility.
|
// should be aware of this possibility.
|
||||||
func (c *Conn) SelectFunc(sql string, onDataRow func(*DataRowReader) error, arguments ...interface{}) (err error) {
|
func (c *Conn) SelectFunc(sql string, onDataRow func(*DataRowReader) error, arguments ...interface{}) error {
|
||||||
defer func() {
|
startTime := time.Now()
|
||||||
if err != nil {
|
err := c.selectFunc(sql, onDataRow, arguments...)
|
||||||
c.logger.Error(fmt.Sprintf("SelectFunc `%s` with %v failed: %v", sql, arguments, err))
|
if err != nil {
|
||||||
}
|
c.logger.Error("SelectFunc", "sql", sql, "args", arguments, "error", err)
|
||||||
}()
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
endTime := time.Now()
|
||||||
|
c.logger.Info("SelectFunc", "sql", sql, "args", arguments, "time", endTime.Sub(startTime))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Conn) selectFunc(sql string, onDataRow func(*DataRowReader) error, arguments ...interface{}) (err error) {
|
||||||
var fields []FieldDescription
|
var fields []FieldDescription
|
||||||
|
|
||||||
if ps, present := c.preparedStatements[sql]; present {
|
if ps, present := c.preparedStatements[sql]; present {
|
||||||
@ -777,9 +784,14 @@ func (c *Conn) sendPreparedQuery(ps *preparedStatement, arguments ...interface{}
|
|||||||
// arguments will be sanitized before being interpolated into sql strings. arguments
|
// arguments will be sanitized before being interpolated into sql strings. arguments
|
||||||
// should be referenced positionally from the sql string as $1, $2, etc.
|
// should be referenced positionally from the sql string as $1, $2, etc.
|
||||||
func (c *Conn) Execute(sql string, arguments ...interface{}) (commandTag CommandTag, err error) {
|
func (c *Conn) Execute(sql string, arguments ...interface{}) (commandTag CommandTag, err error) {
|
||||||
|
startTime := time.Now()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err != nil {
|
if err == nil {
|
||||||
c.logger.Error(fmt.Sprintf("Execute `%s` with %v failed: %v", sql, arguments, err))
|
endTime := time.Now()
|
||||||
|
c.logger.Info("Execute", "sql", sql, "args", arguments, "time", endTime.Sub(startTime))
|
||||||
|
} else {
|
||||||
|
c.logger.Error("Execute", "sql", sql, "args", arguments, "error", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/JackC/pgx"
|
"github.com/JackC/pgx"
|
||||||
|
log "gopkg.in/inconshreveable/log15.v2"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@ -97,24 +97,29 @@ func urlHandler(w http.ResponseWriter, req *http.Request) {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
var err error
|
||||||
connConfig := pgx.ConnConfig{
|
connPoolConfig := pgx.ConnPoolConfig{
|
||||||
Host: "127.0.0.1",
|
ConnConfig: pgx.ConnConfig{
|
||||||
User: "jack",
|
Host: "127.0.0.1",
|
||||||
Password: "jack",
|
User: "jack",
|
||||||
Database: "url_shortener"}
|
Password: "jack",
|
||||||
poolOptions := pgx.ConnPoolConfig{MaxConnections: 5, AfterConnect: afterConnect}
|
Database: "url_shortener",
|
||||||
pool, err = pgx.NewConnPool(connConfig, poolOptions)
|
Logger: log.New("module", "pgx"),
|
||||||
|
},
|
||||||
|
MaxConnections: 5,
|
||||||
|
AfterConnect: afterConnect,
|
||||||
|
}
|
||||||
|
pool, err = pgx.NewConnPool(connPoolConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Unable to create connection pool: %v\n", err)
|
log.Crit("Unable to create connection pool", "error", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
http.HandleFunc("/", urlHandler)
|
http.HandleFunc("/", urlHandler)
|
||||||
|
|
||||||
fmt.Println("Starting URL shortener on localhost:8080...")
|
log.Info("Starting URL shortener on localhost:8080")
|
||||||
err = http.ListenAndServe("localhost:8080", nil)
|
err = http.ListenAndServe("localhost:8080", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Unable to start web server: %v\n", err)
|
log.Crit("Unable to start web server", "error", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user