Replace logging functionality with log15

gopkg.in/inconshreveable/log15.v2
scan-io
Jack Christensen 2014-06-02 19:47:09 -05:00
parent 72f584d993
commit cbddbb423e
3 changed files with 11 additions and 41 deletions

10
conn.go
View File

@ -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:

View File

@ -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()
}

View File

@ -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) }