mirror of
https://github.com/jackc/pgx.git
synced 2025-05-30 03:03:10 +00:00
Replace logging functionality with log15
gopkg.in/inconshreveable/log15.v2
This commit is contained in:
parent
72f584d993
commit
cbddbb423e
10
conn.go
10
conn.go
@ -12,6 +12,7 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
log "gopkg.in/inconshreveable/log15.v2"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
@ -41,7 +42,7 @@ type ConnConfig struct {
|
|||||||
Password string
|
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
|
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
|
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.
|
// Conn is a PostgreSQL connection handle. It is not safe for concurrent usage.
|
||||||
@ -62,7 +63,7 @@ type Conn struct {
|
|||||||
notifications []*Notification
|
notifications []*Notification
|
||||||
alive bool
|
alive bool
|
||||||
causeOfDeath error
|
causeOfDeath error
|
||||||
logger Logger
|
logger log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
type preparedStatement struct {
|
type preparedStatement struct {
|
||||||
@ -130,7 +131,8 @@ func Connect(config ConnConfig) (c *Conn, err error) {
|
|||||||
if c.config.Logger != nil {
|
if c.config.Logger != nil {
|
||||||
c.logger = c.config.Logger
|
c.logger = c.config.Logger
|
||||||
} else {
|
} else {
|
||||||
c.logger = nullLogger("null")
|
c.logger = log.New()
|
||||||
|
c.logger.SetHandler(log.DiscardHandler())
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.config.User == "" {
|
if c.config.User == "" {
|
||||||
@ -219,7 +221,7 @@ func Connect(config ConnConfig) (c *Conn, err error) {
|
|||||||
}
|
}
|
||||||
case readyForQuery:
|
case readyForQuery:
|
||||||
c.rxReadyForQuery(r)
|
c.rxReadyForQuery(r)
|
||||||
c.logger = newPidLogger(c.Pid, c.logger)
|
c.logger = c.logger.New("pid", c.Pid)
|
||||||
c.logger.Info("Connection established")
|
c.logger.Info("Connection established")
|
||||||
return c, nil
|
return c, nil
|
||||||
default:
|
default:
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package pgx
|
package pgx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
log "gopkg.in/inconshreveable/log15.v2"
|
||||||
"io"
|
"io"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
@ -18,7 +19,7 @@ type ConnPool struct {
|
|||||||
config ConnConfig // config used when establishing connection
|
config ConnConfig // config used when establishing connection
|
||||||
maxConnections int
|
maxConnections int
|
||||||
afterConnect func(*Conn) error
|
afterConnect func(*Conn) error
|
||||||
logger Logger
|
logger log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConnPoolStat struct {
|
type ConnPoolStat struct {
|
||||||
@ -37,7 +38,8 @@ func NewConnPool(config ConnPoolConfig) (p *ConnPool, err error) {
|
|||||||
if config.Logger != nil {
|
if config.Logger != nil {
|
||||||
p.logger = config.Logger
|
p.logger = config.Logger
|
||||||
} else {
|
} else {
|
||||||
p.logger = nullLogger("null")
|
p.logger = log.New()
|
||||||
|
p.logger.SetHandler(log.DiscardHandler())
|
||||||
}
|
}
|
||||||
|
|
||||||
p.allConnections = make([]*Conn, 0, p.maxConnections)
|
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
|
// All connections are in use and we cannot create more
|
||||||
if len(p.availableConnections) == 0 {
|
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 {
|
for len(p.availableConnections) == 0 {
|
||||||
p.cond.Wait()
|
p.cond.Wait()
|
||||||
}
|
}
|
||||||
|
34
logger.go
34
logger.go
@ -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) }
|
|
Loading…
x
Reference in New Issue
Block a user