diff --git a/bench_test.go b/bench_test.go index eb9c0595..99f65c2b 100644 --- a/bench_test.go +++ b/bench_test.go @@ -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) - } -} diff --git a/conn.go b/conn.go index e5c2a401..6dfd5d5a 100644 --- a/conn.go +++ b/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. diff --git a/conn_pool.go b/conn_pool.go index a72d5daf..1627af10 100644 --- a/conn_pool.go +++ b/conn_pool.go @@ -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 diff --git a/conn_test.go b/conn_test.go index 4067118c..62e8b7d5 100644 --- a/conn_test.go +++ b/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) { diff --git a/doc.go b/doc.go index 0fd3d2f6..7964aa82 100644 --- a/doc.go +++ b/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 diff --git a/logger.go b/logger.go index f85d4bd0..8cadee4e 100644 --- a/logger.go +++ b/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 diff --git a/v3.md b/v3.md index 5bb7162a..75c9385c 100644 --- a/v3.md +++ b/v3.md @@ -3,3 +3,5 @@ Rename Oid to OID in accordance with Go conventions. Rename Json(b) to JSON(B) in accordance with Go conventions. + +Logger interface reduced to single Log method.