diff --git a/bench_test.go b/bench_test.go index 765547d7..6ff2e239 100644 --- a/bench_test.go +++ b/bench_test.go @@ -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) } diff --git a/conn.go b/conn.go index ed72ff01..e7c5ef94 100644 --- a/conn.go +++ b/conn.go @@ -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) + `"` } diff --git a/conn_test.go b/conn_test.go index a506349c..010cfa7b 100644 --- a/conn_test.go +++ b/conn_test.go @@ -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()