Remove Conn SetLogger and SetLogLevel

Simplifying interface
pull/586/head
Jack Christensen 2019-08-17 15:34:29 -05:00
parent 99c54fbec0
commit f3c703a102
3 changed files with 32 additions and 121 deletions

View File

@ -205,45 +205,49 @@ func (dl discardLogger) Log(ctx context.Context, level pgx.LogLevel, msg string,
}
func BenchmarkSelectWithLoggingTraceDiscard(b *testing.B) {
conn := mustConnect(b, mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE")))
defer closeConn(b, conn)
var logger discardLogger
conn.SetLogger(logger)
conn.SetLogLevel(pgx.LogLevelTrace)
config := mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))
config.Logger = logger
config.LogLevel = pgx.LogLevelTrace
conn := mustConnect(b, config)
defer closeConn(b, conn)
benchmarkSelectWithLog(b, conn)
}
func BenchmarkSelectWithLoggingDebugWithDiscard(b *testing.B) {
conn := mustConnect(b, mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE")))
defer closeConn(b, conn)
var logger discardLogger
conn.SetLogger(logger)
conn.SetLogLevel(pgx.LogLevelDebug)
config := mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))
config.Logger = logger
config.LogLevel = pgx.LogLevelDebug
conn := mustConnect(b, config)
defer closeConn(b, conn)
benchmarkSelectWithLog(b, conn)
}
func BenchmarkSelectWithLoggingInfoWithDiscard(b *testing.B) {
conn := mustConnect(b, mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE")))
defer closeConn(b, conn)
var logger discardLogger
conn.SetLogger(logger)
conn.SetLogLevel(pgx.LogLevelInfo)
config := mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))
config.Logger = logger
config.LogLevel = pgx.LogLevelInfo
conn := mustConnect(b, config)
defer closeConn(b, conn)
benchmarkSelectWithLog(b, conn)
}
func BenchmarkSelectWithLoggingErrorWithDiscard(b *testing.B) {
conn := mustConnect(b, mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE")))
defer closeConn(b, conn)
var logger discardLogger
conn.SetLogger(logger)
conn.SetLogLevel(pgx.LogLevelError)
config := mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))
config.Logger = logger
config.LogLevel = pgx.LogLevelError
conn := mustConnect(b, config)
defer closeConn(b, conn)
benchmarkSelectWithLog(b, conn)
}

20
conn.go
View File

@ -317,26 +317,6 @@ func (c *Conn) log(ctx context.Context, lvl LogLevel, msg string, data map[strin
c.logger.Log(ctx, lvl, msg, data)
}
// SetLogger replaces the current logger and returns the previous logger.
func (c *Conn) SetLogger(logger Logger) Logger {
oldLogger := c.logger
c.logger = logger
return oldLogger
}
// SetLogLevel replaces the current log level and returns the previous log
// level.
func (c *Conn) SetLogLevel(lvl LogLevel) (LogLevel, error) {
oldLvl := c.logLevel
if lvl < LogLevelNone || lvl > LogLevelTrace {
return oldLvl, ErrInvalidLogLevel
}
c.logLevel = lvl
return lvl, nil
}
func quoteIdentifier(s string) string {
return `"` + strings.Replace(s, `"`, `""`, -1) + `"`
}

View File

@ -551,23 +551,23 @@ func (l *testLogger) Log(ctx context.Context, level pgx.LogLevel, msg string, da
func TestLogPassesContext(t *testing.T) {
t.Parallel()
conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE"))
l1 := &testLogger{}
config := mustParseConfig(t, os.Getenv("PGX_TEST_DATABASE"))
config.Logger = l1
conn := mustConnect(t, config)
defer closeConn(t, conn)
ctx := context.WithValue(context.Background(), "ctxdata", "foo")
l1.logs = l1.logs[0:0] // Clear logs written when establishing connection
l1 := &testLogger{}
oldLogger := conn.SetLogger(l1)
if oldLogger != nil {
t.Fatalf("Expected conn.SetLogger to return %v, but it was %v", nil, oldLogger)
}
ctx := context.WithValue(context.Background(), "ctxdata", "foo")
if _, err := conn.Exec(ctx, ";"); err != nil {
t.Fatal(err)
}
if len(l1.logs) != 1 {
t.Fatal("Expected new logger l1 to be called once, but it wasn't")
t.Fatal("Expected logger to be called once, but it wasn't")
}
if l1.logs[0].data["ctxdata"] != "foo" {
@ -575,79 +575,6 @@ func TestLogPassesContext(t *testing.T) {
}
}
func TestSetLogger(t *testing.T) {
t.Parallel()
conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE"))
defer closeConn(t, conn)
l1 := &testLogger{}
oldLogger := conn.SetLogger(l1)
if oldLogger != nil {
t.Fatalf("Expected conn.SetLogger to return %v, but it was %v", nil, oldLogger)
}
if _, err := conn.Exec(context.Background(), "listen foo"); err != nil {
t.Fatal(err)
}
if len(l1.logs) == 0 {
t.Fatal("Expected new logger l1 to be called, but it wasn't")
}
l2 := &testLogger{}
oldLogger = conn.SetLogger(l2)
if oldLogger != l1 {
t.Fatalf("Expected conn.SetLogger to return %v, but it was %v", l1, oldLogger)
}
if _, err := conn.Exec(context.Background(), "listen bar"); err != nil {
t.Fatal(err)
}
if len(l2.logs) == 0 {
t.Fatal("Expected new logger l2 to be called, but it wasn't")
}
}
func TestSetLogLevel(t *testing.T) {
t.Parallel()
conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE"))
defer closeConn(t, conn)
logger := &testLogger{}
conn.SetLogger(logger)
if _, err := conn.SetLogLevel(0); err != pgx.ErrInvalidLogLevel {
t.Fatal("SetLogLevel with invalid level did not return error")
}
if _, err := conn.SetLogLevel(pgx.LogLevelNone); err != nil {
t.Fatal(err)
}
if _, err := conn.Exec(context.Background(), "listen foo"); err != nil {
t.Fatal(err)
}
if len(logger.logs) != 0 {
t.Fatalf("Expected logger not to be called, but it was: %v", logger.logs)
}
if _, err := conn.SetLogLevel(pgx.LogLevelTrace); err != nil {
t.Fatal(err)
}
if _, err := conn.Exec(context.Background(), "listen bar"); err != nil {
t.Fatal(err)
}
if len(logger.logs) == 0 {
t.Fatal("Expected logger to be called, but it wasn't")
}
}
func TestIdentifierSanitize(t *testing.T) {
t.Parallel()