Documentation
¶
Overview ¶
Package slog adapts pgx's tracelog.Logger to the standard library log/slog.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger forwards pgx tracelog records to a *slog.Logger.
func NewLogger ¶
NewLogger returns a Logger that forwards pgx tracelog records to logger.
Example ¶
package main
import (
"context"
"log/slog"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jackc/pgx/v5/tracelog"
slogadapter "github.com/triple-a/gocom/database/pgx-slog"
)
func main() {
textAdapter := slog.DiscardHandler
logger := slog.New(textAdapter)
pgxPool, _ := pgxpool.New(
context.Background(),
"postgres://postgres:postgres@localhost:5432/datawarehouse", // pragma: allowlist secret
)
pgxPool.Config().ConnConfig.Tracer = &tracelog.TraceLog{
Logger: slogadapter.NewLogger(logger),
LogLevel: tracelog.LogLevelTrace,
}
}
Output:
func (*Logger) Log ¶
func (pl *Logger) Log( ctx context.Context, level tracelog.LogLevel, msg string, data map[string]any, )
Log implements tracelog.Logger by forwarding to the underlying *slog.Logger. LogLevelNone is honored as a no-op. Trace and undefined pgx levels are emitted at slog.LevelDebug and slog.LevelError respectively, with an extra PGX_LOG_LEVEL attribute identifying the original pgx level. Attributes are emitted in deterministic (sorted) key order.
Click to show internal directories.
Click to hide internal directories.