diff --git a/conn.go b/conn.go index 72ea7791..ae450d36 100644 --- a/conn.go +++ b/conn.go @@ -12,6 +12,7 @@ import ( "encoding/hex" "errors" "fmt" + log "gopkg.in/inconshreveable/log15.v2" "io" "io/ioutil" "net" @@ -41,7 +42,7 @@ type ConnConfig struct { Password string MsgBufSize int // Size of work buffer used for transcoding messages. For optimal performance, it should be large enough to store a single row from any result set. Default: 1024 TLSConfig *tls.Config // config for TLS connection -- nil disables TLS - Logger Logger + Logger log.Logger } // Conn is a PostgreSQL connection handle. It is not safe for concurrent usage. @@ -62,7 +63,7 @@ type Conn struct { notifications []*Notification alive bool causeOfDeath error - logger Logger + logger log.Logger } type preparedStatement struct { @@ -130,7 +131,8 @@ func Connect(config ConnConfig) (c *Conn, err error) { if c.config.Logger != nil { c.logger = c.config.Logger } else { - c.logger = nullLogger("null") + c.logger = log.New() + c.logger.SetHandler(log.DiscardHandler()) } if c.config.User == "" { @@ -219,7 +221,7 @@ func Connect(config ConnConfig) (c *Conn, err error) { } case readyForQuery: c.rxReadyForQuery(r) - c.logger = newPidLogger(c.Pid, c.logger) + c.logger = c.logger.New("pid", c.Pid) c.logger.Info("Connection established") return c, nil default: diff --git a/conn_pool.go b/conn_pool.go index 24b2b9af..23c5c9db 100644 --- a/conn_pool.go +++ b/conn_pool.go @@ -1,6 +1,7 @@ package pgx import ( + log "gopkg.in/inconshreveable/log15.v2" "io" "sync" ) @@ -18,7 +19,7 @@ type ConnPool struct { config ConnConfig // config used when establishing connection maxConnections int afterConnect func(*Conn) error - logger Logger + logger log.Logger } type ConnPoolStat struct { @@ -37,7 +38,8 @@ func NewConnPool(config ConnPoolConfig) (p *ConnPool, err error) { if config.Logger != nil { p.logger = config.Logger } else { - p.logger = nullLogger("null") + p.logger = log.New() + p.logger.SetHandler(log.DiscardHandler()) } p.allConnections = make([]*Conn, 0, p.maxConnections) @@ -80,7 +82,7 @@ func (p *ConnPool) Acquire() (c *Conn, err error) { // All connections are in use and we cannot create more if len(p.availableConnections) == 0 { - p.logger.Warning("All connections in pool are busy - waiting...") + p.logger.Warn("All connections in pool are busy - waiting...") for len(p.availableConnections) == 0 { p.cond.Wait() } diff --git a/logger.go b/logger.go deleted file mode 100644 index 662cf96f..00000000 --- a/logger.go +++ /dev/null @@ -1,34 +0,0 @@ -package pgx - -import ( - "strconv" -) - -type Logger interface { - Error(msg string) - Warning(msg string) - Info(msg string) - Debug(msg string) -} - -type nullLogger string - -func (l nullLogger) Error(msg string) {} -func (l nullLogger) Warning(msg string) {} -func (l nullLogger) Info(msg string) {} -func (l nullLogger) Debug(msg string) {} - -type pidLogger struct { - prefix string - baseLogger Logger -} - -func newPidLogger(pid int32, baseLogger Logger) *pidLogger { - prefix := "(" + strconv.FormatInt(int64(pid), 10) + ") " - return &pidLogger{prefix: prefix, baseLogger: baseLogger} -} - -func (l *pidLogger) Error(msg string) { l.baseLogger.Error(l.prefix + msg) } -func (l *pidLogger) Warning(msg string) { l.baseLogger.Warning(l.prefix + msg) } -func (l *pidLogger) Info(msg string) { l.baseLogger.Info(l.prefix + msg) } -func (l *pidLogger) Debug(msg string) { l.baseLogger.Debug(l.prefix + msg) }