mirror of https://github.com/jackc/pgx.git
Reduce Logger interface to Log method
parent
04c02cf3d3
commit
390f75c0e1
|
@ -5,7 +5,6 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/jackc/pgx"
|
||||
log "gopkg.in/inconshreveable/log15.v2"
|
||||
)
|
||||
|
||||
func BenchmarkConnPool(b *testing.B) {
|
||||
|
@ -294,71 +293,51 @@ func BenchmarkSelectWithoutLogging(b *testing.B) {
|
|||
benchmarkSelectWithLog(b, conn)
|
||||
}
|
||||
|
||||
func BenchmarkSelectWithLoggingTraceWithLog15(b *testing.B) {
|
||||
connConfig := *defaultConnConfig
|
||||
type discardLogger struct{}
|
||||
|
||||
logger := log.New()
|
||||
lvl, err := log.LvlFromString("debug")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
logger.SetHandler(log.LvlFilterHandler(lvl, log.DiscardHandler()))
|
||||
connConfig.Logger = logger
|
||||
connConfig.LogLevel = pgx.LogLevelTrace
|
||||
conn := mustConnect(b, connConfig)
|
||||
func (dl discardLogger) Log(level int, msg string, ctx ...interface{}) {}
|
||||
|
||||
func BenchmarkSelectWithLoggingTraceDiscard(b *testing.B) {
|
||||
conn := mustConnect(b, *defaultConnConfig)
|
||||
defer closeConn(b, conn)
|
||||
|
||||
var logger discardLogger
|
||||
conn.SetLogger(logger)
|
||||
conn.SetLogLevel(pgx.LogLevelTrace)
|
||||
|
||||
benchmarkSelectWithLog(b, conn)
|
||||
}
|
||||
|
||||
func BenchmarkSelectWithLoggingDebugWithLog15(b *testing.B) {
|
||||
connConfig := *defaultConnConfig
|
||||
|
||||
logger := log.New()
|
||||
lvl, err := log.LvlFromString("debug")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
logger.SetHandler(log.LvlFilterHandler(lvl, log.DiscardHandler()))
|
||||
connConfig.Logger = logger
|
||||
connConfig.LogLevel = pgx.LogLevelDebug
|
||||
conn := mustConnect(b, connConfig)
|
||||
func BenchmarkSelectWithLoggingDebugWithDiscard(b *testing.B) {
|
||||
conn := mustConnect(b, *defaultConnConfig)
|
||||
defer closeConn(b, conn)
|
||||
|
||||
var logger discardLogger
|
||||
conn.SetLogger(logger)
|
||||
conn.SetLogLevel(pgx.LogLevelDebug)
|
||||
|
||||
benchmarkSelectWithLog(b, conn)
|
||||
}
|
||||
|
||||
func BenchmarkSelectWithLoggingInfoWithLog15(b *testing.B) {
|
||||
connConfig := *defaultConnConfig
|
||||
|
||||
logger := log.New()
|
||||
lvl, err := log.LvlFromString("info")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
logger.SetHandler(log.LvlFilterHandler(lvl, log.DiscardHandler()))
|
||||
connConfig.Logger = logger
|
||||
connConfig.LogLevel = pgx.LogLevelInfo
|
||||
conn := mustConnect(b, connConfig)
|
||||
func BenchmarkSelectWithLoggingInfoWithDiscard(b *testing.B) {
|
||||
conn := mustConnect(b, *defaultConnConfig)
|
||||
defer closeConn(b, conn)
|
||||
|
||||
var logger discardLogger
|
||||
conn.SetLogger(logger)
|
||||
conn.SetLogLevel(pgx.LogLevelInfo)
|
||||
|
||||
benchmarkSelectWithLog(b, conn)
|
||||
}
|
||||
|
||||
func BenchmarkSelectWithLoggingErrorWithLog15(b *testing.B) {
|
||||
connConfig := *defaultConnConfig
|
||||
|
||||
logger := log.New()
|
||||
lvl, err := log.LvlFromString("error")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
logger.SetHandler(log.LvlFilterHandler(lvl, log.DiscardHandler()))
|
||||
connConfig.Logger = logger
|
||||
connConfig.LogLevel = pgx.LogLevelError
|
||||
conn := mustConnect(b, connConfig)
|
||||
func BenchmarkSelectWithLoggingErrorWithDiscard(b *testing.B) {
|
||||
conn := mustConnect(b, *defaultConnConfig)
|
||||
defer closeConn(b, conn)
|
||||
|
||||
var logger discardLogger
|
||||
conn.SetLogger(logger)
|
||||
conn.SetLogLevel(pgx.LogLevelError)
|
||||
|
||||
benchmarkSelectWithLog(b, conn)
|
||||
}
|
||||
|
||||
|
@ -418,17 +397,3 @@ func benchmarkSelectWithLog(b *testing.B, conn *pgx.Conn) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkLog15Discard(b *testing.B) {
|
||||
logger := log.New()
|
||||
lvl, err := log.LvlFromString("error")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
logger.SetHandler(log.LvlFilterHandler(lvl, log.DiscardHandler()))
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
logger.Debug("benchmark", "i", i, "b.N", b.N)
|
||||
}
|
||||
}
|
||||
|
|
13
conn.go
13
conn.go
|
@ -1252,18 +1252,7 @@ func (c *Conn) log(lvl int, msg string, ctx ...interface{}) {
|
|||
ctx = append(ctx, "pid", c.Pid)
|
||||
}
|
||||
|
||||
switch lvl {
|
||||
case LogLevelTrace:
|
||||
c.logger.Debug(msg, ctx...)
|
||||
case LogLevelDebug:
|
||||
c.logger.Debug(msg, ctx...)
|
||||
case LogLevelInfo:
|
||||
c.logger.Info(msg, ctx...)
|
||||
case LogLevelWarn:
|
||||
c.logger.Warn(msg, ctx...)
|
||||
case LogLevelError:
|
||||
c.logger.Error(msg, ctx...)
|
||||
}
|
||||
c.logger.Log(lvl, msg, ctx...)
|
||||
}
|
||||
|
||||
// SetLogger replaces the current logger and returns the previous logger.
|
||||
|
|
|
@ -148,7 +148,7 @@ func (p *ConnPool) acquire(deadline *time.Time) (*Conn, error) {
|
|||
} else {
|
||||
// All connections are in use and we cannot create more
|
||||
if p.logLevel >= LogLevelWarn {
|
||||
p.logger.Warn("All connections in pool are busy - waiting...")
|
||||
p.logger.Log(LogLevelWarn, "All connections in pool are busy - waiting...")
|
||||
}
|
||||
|
||||
// Wait until there is an available connection OR room to create a new connection
|
||||
|
|
13
conn_test.go
13
conn_test.go
|
@ -1401,17 +1401,8 @@ type testLogger struct {
|
|||
logs []testLog
|
||||
}
|
||||
|
||||
func (l *testLogger) Debug(msg string, ctx ...interface{}) {
|
||||
l.logs = append(l.logs, testLog{lvl: pgx.LogLevelDebug, msg: msg, ctx: ctx})
|
||||
}
|
||||
func (l *testLogger) Info(msg string, ctx ...interface{}) {
|
||||
l.logs = append(l.logs, testLog{lvl: pgx.LogLevelInfo, msg: msg, ctx: ctx})
|
||||
}
|
||||
func (l *testLogger) Warn(msg string, ctx ...interface{}) {
|
||||
l.logs = append(l.logs, testLog{lvl: pgx.LogLevelWarn, msg: msg, ctx: ctx})
|
||||
}
|
||||
func (l *testLogger) Error(msg string, ctx ...interface{}) {
|
||||
l.logs = append(l.logs, testLog{lvl: pgx.LogLevelError, msg: msg, ctx: ctx})
|
||||
func (l *testLogger) Log(level int, msg string, ctx ...interface{}) {
|
||||
l.logs = append(l.logs, testLog{lvl: level, msg: msg, ctx: ctx})
|
||||
}
|
||||
|
||||
func TestSetLogger(t *testing.T) {
|
||||
|
|
4
doc.go
4
doc.go
|
@ -202,9 +202,7 @@ connection.
|
|||
Logging
|
||||
|
||||
pgx defines a simple logger interface. Connections optionally accept a logger
|
||||
that satisfies this interface. The log15 package
|
||||
(http://gopkg.in/inconshreveable/log15.v2) satisfies this interface and it is
|
||||
simple to define adapters for other loggers. Set LogLevel to control logging
|
||||
that satisfies this interface. Set LogLevel to control logging
|
||||
verbosity.
|
||||
*/
|
||||
package pgx
|
||||
|
|
11
logger.go
11
logger.go
|
@ -7,8 +7,7 @@ import (
|
|||
)
|
||||
|
||||
// The values for log levels are chosen such that the zero value means that no
|
||||
// log level was specified and we can default to LogLevelDebug to preserve
|
||||
// the behavior that existed prior to log level introduction.
|
||||
// log level was specified.
|
||||
const (
|
||||
LogLevelTrace = 6
|
||||
LogLevelDebug = 5
|
||||
|
@ -19,15 +18,9 @@ const (
|
|||
)
|
||||
|
||||
// Logger is the interface used to get logging from pgx internals.
|
||||
// https://github.com/inconshreveable/log15 is the recommended logging package.
|
||||
// This logging interface was extracted from there. However, it should be simple
|
||||
// to adapt any logger to this interface.
|
||||
type Logger interface {
|
||||
// Log a message at the given level with context key/value pairs
|
||||
Debug(msg string, ctx ...interface{})
|
||||
Info(msg string, ctx ...interface{})
|
||||
Warn(msg string, ctx ...interface{})
|
||||
Error(msg string, ctx ...interface{})
|
||||
Log(level int, msg string, ctx ...interface{})
|
||||
}
|
||||
|
||||
// LogLevelFromString converts log level string to constant
|
||||
|
|
Loading…
Reference in New Issue