pg_fancylog is a query tracer for pgx/v5 that uses fancylog for beautiful, structured logging of database queries.
- Query Tracing: Automatically logs SQL queries, arguments, and execution duration.
- Visual Clarity: Uses
fancylogto provide stylized and readable log output. - Result Tracking: Logs rows affected for DML statements (INSERT, UPDATE, DELETE) and rows returned for others.
- Error Logging: Captures and logs database errors with full query context.
- Easy Integration: Simple wrapper functions to create
pgxpoolinstances with tracing enabled.
go get gitlab.wg.nask.world/nask/pg_fancylog.gitThe easiest way to get started is using NewPoolWithTrace:
package main
import (
"context"
"os"
"github.com/n-ask/fancylog"
"gitlab.wg.nask.world/nask/pg_fancylog.git"
)
func main() {
ctx := context.Background()
log := fancylog.New(os.Stdout)
dbURL := "postgres://user:password@localhost:5432/dbname"
pool, err := pg_fancylog.NewPoolWithTrace(ctx, log, dbURL)
if err != nil {
log.Fatal(err)
}
defer pool.Close()
// All queries executed via this pool will now be logged by fancylog
}If you need to customize the pool configuration, use NewTracePoolWithConfig:
package main
import (
"context"
"os"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/n-ask/fancylog"
"gitlab.wg.nask.world/nask/pg_fancylog.git"
)
func main() {
ctx := context.Background()
log := fancylog.New(os.Stdout)
config, _ := pgxpool.ParseConfig("postgres://user:password@localhost:5432/dbname")
config.MaxConns = 10
config.MaxConnIdleTime = 5 * time.Minute
pool, err := pg_fancylog.NewTracePoolWithConfig(ctx, log, config)
if err != nil {
log.Fatal(err)
}
defer pool.Close()
}When a query is executed, pg_fancylog will output a structured log map similar to:
Errors will be logged at the ERROR level: