https://github.com/pgx-contrib/pgxslog
Structured log/slog adapter for pgx v5 — logs queries, batches, and connections with snake_case keys, SQL name extraction, and per-request logger injection
https://github.com/pgx-contrib/pgxslog
database golang logging observability pgx pgxpool postgresql slog structured-logging tracelog
Last synced: 28 days ago
JSON representation
Structured log/slog adapter for pgx v5 — logs queries, batches, and connections with snake_case keys, SQL name extraction, and per-request logger injection
- Host: GitHub
- URL: https://github.com/pgx-contrib/pgxslog
- Owner: pgx-contrib
- License: mit
- Created: 2024-03-03T05:06:48.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2026-05-10T12:32:46.000Z (about 1 month ago)
- Last Synced: 2026-05-10T14:28:44.554Z (about 1 month ago)
- Topics: database, golang, logging, observability, pgx, pgxpool, postgresql, slog, structured-logging, tracelog
- Language: Go
- Homepage: https://pkg.go.dev/github.com/pgx-contrib/pgxslog
- Size: 55.7 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pgxslog
[](https://github.com/pgx-contrib/pgxslog/actions/workflows/ci.yml)
[](https://github.com/pgx-contrib/pgxslog/releases)
[](https://pkg.go.dev/github.com/pgx-contrib/pgxslog)
[](LICENSE)
[](https://go.dev)
[](https://github.com/jackc/pgx)
`Logger` is a [`log/slog`](https://pkg.go.dev/log/slog) adapter for [pgx v5](https://github.com/jackc/pgx)
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.
## Installation
```bash
go get github.com/pgx-contrib/pgxslog
```
## Usage
### Connection pool
```go
config, 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()
```
### Per-request logger via context
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()`:
```go
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,
}
```
## Development
### DevContainer
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
```bash
nix develop # enter shell with Go
go tool ginkgo run -r
```
### Run tests
```bash
# 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
```
## License
[MIT](LICENSE)