Reduce Logger interface to Log method

v3-experimental-wait-ping-context
Jack Christensen 2016-08-02 14:42:31 -05:00
parent 04c02cf3d3
commit 390f75c0e1
7 changed files with 36 additions and 98 deletions

View File

@ -5,7 +5,6 @@ import (
"time" "time"
"github.com/jackc/pgx" "github.com/jackc/pgx"
log "gopkg.in/inconshreveable/log15.v2"
) )
func BenchmarkConnPool(b *testing.B) { func BenchmarkConnPool(b *testing.B) {
@ -294,71 +293,51 @@ func BenchmarkSelectWithoutLogging(b *testing.B) {
benchmarkSelectWithLog(b, conn) benchmarkSelectWithLog(b, conn)
} }
func BenchmarkSelectWithLoggingTraceWithLog15(b *testing.B) { type discardLogger struct{}
connConfig := *defaultConnConfig
logger := log.New() func (dl discardLogger) Log(level int, msg string, ctx ...interface{}) {}
lvl, err := log.LvlFromString("debug")
if err != nil { func BenchmarkSelectWithLoggingTraceDiscard(b *testing.B) {
b.Fatal(err) conn := mustConnect(b, *defaultConnConfig)
}
logger.SetHandler(log.LvlFilterHandler(lvl, log.DiscardHandler()))
connConfig.Logger = logger
connConfig.LogLevel = pgx.LogLevelTrace
conn := mustConnect(b, connConfig)
defer closeConn(b, conn) defer closeConn(b, conn)
var logger discardLogger
conn.SetLogger(logger)
conn.SetLogLevel(pgx.LogLevelTrace)
benchmarkSelectWithLog(b, conn) benchmarkSelectWithLog(b, conn)
} }
func BenchmarkSelectWithLoggingDebugWithLog15(b *testing.B) { func BenchmarkSelectWithLoggingDebugWithDiscard(b *testing.B) {
connConfig := *defaultConnConfig conn := mustConnect(b, *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)
defer closeConn(b, conn) defer closeConn(b, conn)
var logger discardLogger
conn.SetLogger(logger)
conn.SetLogLevel(pgx.LogLevelDebug)
benchmarkSelectWithLog(b, conn) benchmarkSelectWithLog(b, conn)
} }
func BenchmarkSelectWithLoggingInfoWithLog15(b *testing.B) { func BenchmarkSelectWithLoggingInfoWithDiscard(b *testing.B) {
connConfig := *defaultConnConfig conn := mustConnect(b, *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)
defer closeConn(b, conn) defer closeConn(b, conn)
var logger discardLogger
conn.SetLogger(logger)
conn.SetLogLevel(pgx.LogLevelInfo)
benchmarkSelectWithLog(b, conn) benchmarkSelectWithLog(b, conn)
} }
func BenchmarkSelectWithLoggingErrorWithLog15(b *testing.B) { func BenchmarkSelectWithLoggingErrorWithDiscard(b *testing.B) {
connConfig := *defaultConnConfig conn := mustConnect(b, *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)
defer closeConn(b, conn) defer closeConn(b, conn)
var logger discardLogger
conn.SetLogger(logger)
conn.SetLogLevel(pgx.LogLevelError)
benchmarkSelectWithLog(b, conn) 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
View File

@ -1252,18 +1252,7 @@ func (c *Conn) log(lvl int, msg string, ctx ...interface{}) {
ctx = append(ctx, "pid", c.Pid) ctx = append(ctx, "pid", c.Pid)
} }
switch lvl { c.logger.Log(lvl, msg, ctx...)
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...)
}
} }
// SetLogger replaces the current logger and returns the previous logger. // SetLogger replaces the current logger and returns the previous logger.

View File

@ -148,7 +148,7 @@ func (p *ConnPool) acquire(deadline *time.Time) (*Conn, error) {
} else { } else {
// All connections are in use and we cannot create more // All connections are in use and we cannot create more
if p.logLevel >= LogLevelWarn { 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 // Wait until there is an available connection OR room to create a new connection

View File

@ -1401,17 +1401,8 @@ type testLogger struct {
logs []testLog logs []testLog
} }
func (l *testLogger) Debug(msg string, ctx ...interface{}) { func (l *testLogger) Log(level int, msg string, ctx ...interface{}) {
l.logs = append(l.logs, testLog{lvl: pgx.LogLevelDebug, msg: msg, ctx: ctx}) l.logs = append(l.logs, testLog{lvl: level, 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 TestSetLogger(t *testing.T) { func TestSetLogger(t *testing.T) {

4
doc.go
View File

@ -202,9 +202,7 @@ connection.
Logging Logging
pgx defines a simple logger interface. Connections optionally accept a logger pgx defines a simple logger interface. Connections optionally accept a logger
that satisfies this interface. The log15 package that satisfies this interface. Set LogLevel to control logging
(http://gopkg.in/inconshreveable/log15.v2) satisfies this interface and it is
simple to define adapters for other loggers. Set LogLevel to control logging
verbosity. verbosity.
*/ */
package pgx package pgx

View File

@ -7,8 +7,7 @@ import (
) )
// The values for log levels are chosen such that the zero value means that no // 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 // log level was specified.
// the behavior that existed prior to log level introduction.
const ( const (
LogLevelTrace = 6 LogLevelTrace = 6
LogLevelDebug = 5 LogLevelDebug = 5
@ -19,15 +18,9 @@ const (
) )
// Logger is the interface used to get logging from pgx internals. // 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 { type Logger interface {
// Log a message at the given level with context key/value pairs // Log a message at the given level with context key/value pairs
Debug(msg string, ctx ...interface{}) Log(level int, msg string, ctx ...interface{})
Info(msg string, ctx ...interface{})
Warn(msg string, ctx ...interface{})
Error(msg string, ctx ...interface{})
} }
// LogLevelFromString converts log level string to constant // LogLevelFromString converts log level string to constant

2
v3.md
View File

@ -3,3 +3,5 @@
Rename Oid to OID in accordance with Go conventions. Rename Oid to OID in accordance with Go conventions.
Rename Json(b) to JSON(B) in accordance with Go conventions. Rename Json(b) to JSON(B) in accordance with Go conventions.
Logger interface reduced to single Log method.