Logger is a log/slog adapter for pgx v5
that routes every database event through the standard structured logging pipeline.
Assign it to tracelog.TraceLog and queries, batches, prepared statements, and
connections are logged with snake_case keys, automatic SQL name extraction, and
optional per-request logger injection via context.
go get github.com/pgx-contrib/pgxslogconfig, err := pgxpool.ParseConfig(os.Getenv("PGX_DATABASE_URL"))
if err != nil {
panic(err)
}
config.ConnConfig.Tracer = &tracelog.TraceLog{
Logger: &pgxslog.Logger{},
LogLevel: tracelog.LogLevelTrace,
}
pool, err := pgxpool.NewWithConfig(context.Background(), config)
if err != nil {
panic(err)
}
defer pool.Close()Store a *slog.Logger in the context (e.g. one already enriched with a
request ID) and pgxslog.Logger will use it instead of slog.Default():
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil)).With("request_id", "abc123")
ctx := context.WithValue(ctx, pgxslog.LoggerKey, logger)
config.ConnConfig.Tracer = &tracelog.TraceLog{
Logger: &pgxslog.Logger{ContextKey: pgxslog.LoggerKey},
LogLevel: tracelog.LogLevelInfo,
}Open in VS Code with the Dev Containers extension. The environment provides Go, PostgreSQL 18, and Nix automatically.
PGX_DATABASE_URL=postgres://vscode@postgres:5432/pgxslog?sslmode=disable
nix develop # enter shell with Go
go tool ginkgo run -r# Unit tests only (no database required)
go tool ginkgo run -r
# With integration tests
export PGX_DATABASE_URL="postgres://localhost/pgxslog?sslmode=disable"
go tool ginkgo run -r