Add log adapters for testing and log15

Make LogLevel a type for Stringer interface.
This commit is contained in:
Jack Christensen 2017-04-29 20:33:52 -05:00
parent 353ca7c5c7
commit 855b735eae
8 changed files with 103 additions and 30 deletions

View File

@ -179,7 +179,7 @@ func BenchmarkSelectWithoutLogging(b *testing.B) {
type discardLogger struct{}
func (dl discardLogger) Log(level int, msg string, ctx ...interface{}) {}
func (dl discardLogger) Log(level pgx.LogLevel, msg string, ctx ...interface{}) {}
func BenchmarkSelectWithLoggingTraceDiscard(b *testing.B) {
conn := mustConnect(b, *defaultConnConfig)

View File

@ -1213,7 +1213,7 @@ func (c *Conn) shouldLog(lvl int) bool {
return c.logger != nil && c.logLevel >= lvl
}
func (c *Conn) log(lvl int, msg string, ctx ...interface{}) {
func (c *Conn) log(lvl LogLevel, msg string, ctx ...interface{}) {
if c.pid != 0 {
ctx = append(ctx, "pid", c.PID)
}

View File

@ -1739,7 +1739,7 @@ func TestCatchSimultaneousConnectionQueryAndExec(t *testing.T) {
}
type testLog struct {
lvl int
lvl pgx.LogLevel
msg string
ctx []interface{}
}
@ -1748,7 +1748,7 @@ type testLogger struct {
logs []testLog
}
func (l *testLogger) Log(level int, msg string, ctx ...interface{}) {
func (l *testLogger) Log(level pgx.LogLevel, msg string, ctx ...interface{}) {
l.logs = append(l.logs, testLog{lvl: level, msg: msg, ctx: ctx})
}

View File

@ -1,11 +1,13 @@
package main
import (
"github.com/jackc/pgx"
log "gopkg.in/inconshreveable/log15.v2"
"io/ioutil"
"net/http"
"os"
"github.com/jackc/pgx"
"github.com/jackc/pgx/log/log15adapter"
log "gopkg.in/inconshreveable/log15.v2"
)
var pool *pgx.ConnPool
@ -98,27 +100,8 @@ func urlHandler(w http.ResponseWriter, req *http.Request) {
}
}
type log15Adapter struct {
logger log.Logger
}
func (a *log15Adapter) Log(level int, msg string, ctx ...interface{}) {
switch level {
case pgx.LogLevelTrace, pgx.LogLevelDebug:
a.logger.Debug(msg, ctx...)
case pgx.LogLevelInfo:
a.logger.Info(msg, ctx...)
case pgx.LogLevelWarn:
a.logger.Warn(msg, ctx...)
case pgx.LogLevelError:
a.logger.Error(msg, ctx...)
default:
panic("invalid log level")
}
}
func main() {
logger := &log15Adapter{logger: log.New("module", "pgx")}
logger := log15adapter.NewLogger(log.New("module", "pgx"))
var err error
connPoolConfig := pgx.ConnPoolConfig{

View File

@ -0,0 +1,42 @@
// Package log15adapter provides a logger that writes to a github.com/inconshreveable/log15.Logger
// log.
package log15adapter
import (
"github.com/jackc/pgx"
)
// Log15Logger interface defines the subset of
// github.com/inconshreveable/log15.Logger that this adapter uses.
type Log15Logger interface {
Debug(msg string, ctx ...interface{})
Info(msg string, ctx ...interface{})
Warn(msg string, ctx ...interface{})
Error(msg string, ctx ...interface{})
Crit(msg string, ctx ...interface{})
}
type Logger struct {
l Log15Logger
}
func NewLogger(l Log15Logger) *Logger {
return &Logger{l: l}
}
func (l *Logger) Log(level pgx.LogLevel, msg string, ctx ...interface{}) {
switch level {
case pgx.LogLevelTrace:
l.l.Debug(msg, append(ctx, "PGX_LOG_LEVEL", level)...)
case pgx.LogLevelDebug:
l.l.Debug(msg, ctx...)
case pgx.LogLevelInfo:
l.l.Info(msg, ctx...)
case pgx.LogLevelWarn:
l.l.Warn(msg, ctx...)
case pgx.LogLevelError:
l.l.Error(msg, ctx...)
default:
l.l.Error(msg, append(ctx, "INVALID_PGX_LOG_LEVEL", level)...)
}
}

View File

@ -0,0 +1,25 @@
// Package testingadapter provides a logger that writes to a test or benchmark
// log.
package testingadapter
import (
"github.com/jackc/pgx"
)
// TestingLogger interface defines the subset of testing.TB methods used by this
// adapter.
type TestingLogger interface {
Log(args ...interface{})
}
type Logger struct {
l TestingLogger
}
func NewLogger(l TestingLogger) *Logger {
return &Logger{l: l}
}
func (l *Logger) Log(level pgx.LogLevel, msg string, ctx ...interface{}) {
l.l.Log(level, msg, ctx)
}

View File

@ -17,10 +17,33 @@ const (
LogLevelNone = 1
)
// LogLevel represents the pgx logging level. See LogLevel* constants for
// possible values.
type LogLevel int
func (ll LogLevel) String() string {
switch ll {
case LogLevelTrace:
return "trace"
case LogLevelDebug:
return "debug"
case LogLevelInfo:
return "info"
case LogLevelWarn:
return "warn"
case LogLevelError:
return "error"
case LogLevelNone:
return "none"
default:
return fmt.Sprintf("invalid level %d", ll)
}
}
// Logger is the interface used to get logging from pgx internals.
type Logger interface {
// Log a message at the given level with context key/value pairs
Log(level int, msg string, ctx ...interface{})
Log(level LogLevel, msg string, ctx ...interface{})
}
// LogLevelFromString converts log level string to constant
@ -32,7 +55,7 @@ type Logger interface {
// warn
// error
// none
func LogLevelFromString(s string) (int, error) {
func LogLevelFromString(s string) (LogLevel, error) {
switch s {
case "trace":
return LogLevelTrace, nil

View File

@ -365,7 +365,7 @@ func TestConnQuery(t *testing.T) {
}
type testLog struct {
lvl int
lvl pgx.LogLevel
msg string
ctx []interface{}
}
@ -374,7 +374,7 @@ type testLogger struct {
logs []testLog
}
func (l *testLogger) Log(lvl int, msg string, ctx ...interface{}) {
func (l *testLogger) Log(lvl pgx.LogLevel, msg string, ctx ...interface{}) {
l.logs = append(l.logs, testLog{lvl: lvl, msg: msg, ctx: ctx})
}