logger.go

  1package db
  2
  3import (
  4	"context"
  5	"database/sql"
  6	"strings"
  7
  8	"charm.land/log/v2"
  9	"github.com/jmoiron/sqlx"
 10)
 11
 12func trace(l *log.Logger, query string, args ...interface{}) {
 13	if l != nil {
 14		// Remove newlines and tabs
 15		query = strings.ReplaceAll(query, "\t", "")
 16		query = strings.TrimSpace(query)
 17		l.Debug("trace", "query", query, "args", args)
 18	}
 19}
 20
 21// Select is a wrapper around sqlx.Select that logs the query and arguments.
 22func (d *DB) Select(dest interface{}, query string, args ...interface{}) error {
 23	trace(d.logger, query, args...)
 24	return d.DB.Select(dest, query, args...)
 25}
 26
 27// Get is a wrapper around sqlx.Get that logs the query and arguments.
 28func (d *DB) Get(dest interface{}, query string, args ...interface{}) error {
 29	trace(d.logger, query, args...)
 30	return d.DB.Get(dest, query, args...)
 31}
 32
 33// Queryx is a wrapper around sqlx.Queryx that logs the query and arguments.
 34func (d *DB) Queryx(query string, args ...interface{}) (*sqlx.Rows, error) {
 35	trace(d.logger, query, args...)
 36	return d.DB.Queryx(query, args...)
 37}
 38
 39// QueryRowx is a wrapper around sqlx.QueryRowx that logs the query and arguments.
 40func (d *DB) QueryRowx(query string, args ...interface{}) *sqlx.Row {
 41	trace(d.logger, query, args...)
 42	return d.DB.QueryRowx(query, args...)
 43}
 44
 45// Exec is a wrapper around sqlx.Exec that logs the query and arguments.
 46//
 47// Deprecated: Use [DB.ExecContext] instead.
 48func (d *DB) Exec(query string, args ...interface{}) (sql.Result, error) {
 49	trace(d.logger, query, args...)
 50	return d.DB.Exec(query, args...) //nolint:noctx
 51}
 52
 53// SelectContext is a wrapper around sqlx.SelectContext that logs the query and arguments.
 54func (d *DB) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
 55	trace(d.logger, query, args...)
 56	return d.DB.SelectContext(ctx, dest, query, args...)
 57}
 58
 59// GetContext is a wrapper around sqlx.GetContext that logs the query and arguments.
 60func (d *DB) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
 61	trace(d.logger, query, args...)
 62	return d.DB.GetContext(ctx, dest, query, args...)
 63}
 64
 65// QueryxContext is a wrapper around sqlx.QueryxContext that logs the query and arguments.
 66func (d *DB) QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error) {
 67	trace(d.logger, query, args...)
 68	return d.DB.QueryxContext(ctx, query, args...)
 69}
 70
 71// QueryRowxContext is a wrapper around sqlx.QueryRowxContext that logs the query and arguments.
 72func (d *DB) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row {
 73	trace(d.logger, query, args...)
 74	return d.DB.QueryRowxContext(ctx, query, args...)
 75}
 76
 77// ExecContext is a wrapper around sqlx.ExecContext that logs the query and arguments.
 78func (d *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
 79	trace(d.logger, query, args...)
 80	return d.DB.ExecContext(ctx, query, args...)
 81}
 82
 83// Select is a wrapper around sqlx.Select that logs the query and arguments.
 84func (t *Tx) Select(dest interface{}, query string, args ...interface{}) error {
 85	trace(t.logger, query, args...)
 86	return t.Tx.Select(dest, query, args...)
 87}
 88
 89// Get is a wrapper around sqlx.Get that logs the query and arguments.
 90func (t *Tx) Get(dest interface{}, query string, args ...interface{}) error {
 91	trace(t.logger, query, args...)
 92	return t.Tx.Get(dest, query, args...)
 93}
 94
 95// Queryx is a wrapper around sqlx.Queryx that logs the query and arguments.
 96func (t *Tx) Queryx(query string, args ...interface{}) (*sqlx.Rows, error) {
 97	trace(t.logger, query, args...)
 98	return t.Tx.Queryx(query, args...)
 99}
100
101// QueryRowx is a wrapper around sqlx.QueryRowx that logs the query and arguments.
102func (t *Tx) QueryRowx(query string, args ...interface{}) *sqlx.Row {
103	trace(t.logger, query, args...)
104	return t.Tx.QueryRowx(query, args...)
105}
106
107// Exec is a wrapper around sqlx.Exec that logs the query and arguments.
108//
109// Deprecated: Use [Tx.ExecContext] instead.
110func (t *Tx) Exec(query string, args ...interface{}) (sql.Result, error) {
111	trace(t.logger, query, args...)
112	return t.Tx.Exec(query, args...) //nolint:noctx
113}
114
115// SelectContext is a wrapper around sqlx.SelectContext that logs the query and arguments.
116func (t *Tx) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
117	trace(t.logger, query, args...)
118	return t.Tx.SelectContext(ctx, dest, query, args...)
119}
120
121// GetContext is a wrapper around sqlx.GetContext that logs the query and arguments.
122func (t *Tx) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
123	trace(t.logger, query, args...)
124	return t.Tx.GetContext(ctx, dest, query, args...)
125}
126
127// QueryxContext is a wrapper around sqlx.QueryxContext that logs the query and arguments.
128func (t *Tx) QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error) {
129	trace(t.logger, query, args...)
130	return t.Tx.QueryxContext(ctx, query, args...)
131}
132
133// QueryRowxContext is a wrapper around sqlx.QueryRowxContext that logs the query and arguments.
134func (t *Tx) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row {
135	trace(t.logger, query, args...)
136	return t.Tx.QueryRowxContext(ctx, query, args...)
137}
138
139// ExecContext is a wrapper around sqlx.ExecContext that logs the query and arguments.
140func (t *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
141	trace(t.logger, query, args...)
142	return t.Tx.ExecContext(ctx, query, args...)
143}