Help us shape the future of Go! Take the 2026 Go Developer Survey

loggingdriver

package
v0.3.7 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package loggingdriver wraps a database/sql/driver.Driver to log SQL execution and optionally interpolate placeholders for readable logs.

Adaptor implementations (for example mysql) call Wrap around the native driver before sql.OpenDB, so every Query / Exec / transaction is timed and recorded through logx with driver name, interpolated query text, and cost.

Wrap

dc := loggingdriver.Wrap(mysql.MySQLDriver{}, "mysql",
	loggingdriver.WithDsnParser(ParseDSN),
	loggingdriver.WithErrorLeveler(ErrorLevel),
	loggingdriver.WithInterpolator(loggingdriver.DefaultInterpolate),
)
conn, err := dc.OpenConnector(dsn)

Wrap returns a driver.DriverContext. OpenConnector may rewrite the DSN via WithDsnParser, and marks read-only pools when the DSN query has `_ro=true` (driver name becomes `name::ro` for logs).

Logging behavior

  • QueryContext / ExecContext: optional interpolate, then log cost_ms and query; failures use Error or Warn based on WithErrorLeveler (level > 0 → Error, else Warn); success → Debug
  • BeginTx / Commit / Rollback: transaction boundary Debug lines
  • Prepare is deliberately unimplemented (panics) so dialects stay on context Exec/Query paths

Interpolation

Migrator dry-run output also uses DefaultInterpolate to print a full SQL script from collected fragments.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cost

func Cost() func() time.Duration

func DefaultInterpolate

func DefaultInterpolate(q string, args []driver.NamedValue) (string, []driver.NamedValue)

func Interpolate

func Interpolate(q string, args []driver.NamedValue, loc *time.Location) (string, error)

func NewPrinter

func NewPrinter(q string, args []driver.NamedValue) fmt.Stringer

func OrderedInterpolator

func OrderedInterpolator(q string, args []driver.NamedValue) (string, []driver.NamedValue)

func WithDsnParser

func WithDsnParser(parser func(string) (string, error)) func(*options)

func WithErrorLeveler

func WithErrorLeveler(level func(error) int) func(*options)

func WithInterpolator

func WithInterpolator(interpolator func(string, []driver.NamedValue) (string, []driver.NamedValue)) func(*options)

func Wrap

Types

type DriverOptionApplier

type DriverOptionApplier func(*options)

type Interpolator

type Interpolator func(string, []driver.NamedValue) (string, []driver.NamedValue)

Jump to

Keyboard shortcuts

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