pgxslog

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 13, 2024 License: MIT Imports: 5 Imported by: 0

README

This library aims to be used as a drop-in adapter of pgx for slog

As described in discussion, slog in standard package is expected to be natively integrated in https://github.com/jackc/pgx/ after go 1.21 is released.

also, since golang.org/x/exp/slog is a different package from standard log/slog package, github.com/mcosta74/pgx-slog is not compatible with standard log/slog

example usage


import (
	"context"
	"fmt"
	"log/slog"

	"github.com/jackc/pgx/v5/pgxpool"
	pgxslog "github.com/timtoronto634/pgx-slog"
)

func setupDB(logger *slog.Logger) (*pgxpool.Pool, error) {
	connStr := fmt.Sprintf(
		"postgres://%s:%s@%s:%d/%s?sslmode=disable",
		DBUser, DBPass, DBHost, DBPort, DBName,
	)
	config, err := pgxpool.ParseConfig(connStr)
	if err != nil {
		return nil, err
	}

	config.ConnConfig.Tracer = pgxslog.NewTracer(logger)
	return pgxpool.NewWithConfig(context.Background(), config)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewTracer

func NewTracer(l *slog.Logger) pgx.QueryTracer

Types

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

func (*Logger) Log

func (l *Logger) Log(ctx context.Context, level tracelog.LogLevel, msg string, data map[string]any)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL