sqllogger

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2019 License: MIT Imports: 6 Imported by: 2

README

go-sqllogger

Go Report Card Go Doc GitHub release Build Status

Go SQL driver adapter for logging queries and other SQL operations

  • Any driver.Connector can be wrapped with sqllogger.LoggingConnector(SQLLogger, driver.Connector) to log Connect, Prepare, Exec, Query, Commit, Rollback and Close on database, connection, statement, rows and transaction instances (see ./sql_logger.go for all intercepted calls)
  • The sqllogger.SQLLogger interface can be implemented to log SQL to any logging library
  • sqllogger.NewDefaultSQLLogger(StdLogger) offers a default implementation for the standard library log.Logger or implementations of the StdLogger interface
  • Zero dependencies

Note: The adapter has been tested using github.com/lib/pq. Other SQL drivers might need additional work. As the database/sql/driver package offers a lot of optional interfaces, not every advanced feature might work as expected when using the logging connector as an adapter to the original driver.

Example

This needs a running PostgreSQL database.

See ./example/main.go:

package main

import (
	"context"
	"database/sql"
	"fmt"
	"log"
	"os"

	"github.com/lib/pq"
	"github.com/networkteam/go-sqllogger"
)

func main() {
	logger := log.New(os.Stderr, "SQL: ", 0)

	sqlLogger := sqllogger.NewDefaultSQLLogger(logger)
	sqlLogger.LogClose = true

	pqConnector, err := pq.NewConnector("dbname=test sslmode=disable")
	if err != nil {
		failf("could not connect to database: %v", err)
	}
	connector := sqllogger.LoggingConnector(sqlLogger, pqConnector)

	db := sql.OpenDB(connector)

	ctx := context.Background()

	rows, err := db.QueryContext(ctx, "SELECT 42")
	if err != nil {
		failf("could not query database: %v", err)
	}
	defer rows.Close()

	if rows.Next() {
		var answer int
		if err := rows.Scan(&answer); err != nil {
			failf("could not scan row: %v", err)
		}

		fmt.Printf("The answer is: %d\n", answer)
	}
}

func failf(format string, args ...interface{}) {
	fmt.Printf(format+"\n", args...)
	os.Exit(1)
}

Running the example:

> createdb test # If it does not exist
> go run github.com/networkteam/go-sqllogger/example
SQL: Connect → CONN(1)
SQL: CONN(1) ► Query(SELECT 42) → ROWS(2)
The answer is: 42
SQL: ROWS(2) ► Close

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoggingConnector

func LoggingConnector(log SQLLogger, connector driver.Connector) driver.Connector

LoggingConnector wraps the given driver.Connector and invokes the given SQLLogger for queries and other SQL operations.

Note: Due to the amount of optional interfaces in the database/sql/driver package, there might be some features of the original driver that are not exposed on the returned driver.Connector.

Types

type DefaultSQLLogger added in v0.2.0

type DefaultSQLLogger struct {

	// Enabled sets, whether
	Enabled bool

	LogConnect bool
	LogClose   bool
	// contains filtered or unexported fields
}

DefaultSQLLogger is an implementation of the Logger interface logging to a *log.Logger from the standard library

func NewDefaultSQLLogger added in v0.2.0

func NewDefaultSQLLogger(log StdLogger) *DefaultSQLLogger

NewDefaultSQLLogger creates a new default SQL logger with sensible defaults

A *log.Logger can be passed or any other implementation of the StdLogger interface.

func (*DefaultSQLLogger) ConnBegin added in v0.2.0

func (dl *DefaultSQLLogger) ConnBegin(connID, txID int64, opts driver.TxOptions)

ConnBegin satisfies Logger interface

func (*DefaultSQLLogger) ConnClose added in v0.2.0

func (dl *DefaultSQLLogger) ConnClose(connID int64)

ConnClose satisfies Logger interface

func (*DefaultSQLLogger) ConnExec added in v0.2.0

func (dl *DefaultSQLLogger) ConnExec(connID int64, query string, args []driver.Value)

ConnExec satisfies Logger interface

func (*DefaultSQLLogger) ConnExecContext added in v0.2.0

func (dl *DefaultSQLLogger) ConnExecContext(connID int64, query string, args []driver.NamedValue)

ConnExecContext satisfies Logger interface

func (*DefaultSQLLogger) ConnPrepare added in v0.2.0

func (dl *DefaultSQLLogger) ConnPrepare(connID, stmtID int64, query string)

ConnPrepare satisfies Logger interface

func (*DefaultSQLLogger) ConnPrepareContext added in v0.2.0

func (dl *DefaultSQLLogger) ConnPrepareContext(connID int64, stmtID int64, query string)

ConnPrepareContext satisfies Logger interface

func (*DefaultSQLLogger) ConnQuery added in v0.2.0

func (dl *DefaultSQLLogger) ConnQuery(connID, rowsID int64, query string, args []driver.Value)

ConnQuery satisfies Logger interface

func (*DefaultSQLLogger) ConnQueryContext added in v0.2.0

func (dl *DefaultSQLLogger) ConnQueryContext(connID int64, rowsID int64, query string, args []driver.NamedValue)

ConnQueryContext satisfies Logger interface

func (*DefaultSQLLogger) Connect added in v0.2.0

func (dl *DefaultSQLLogger) Connect(connID int64)

Connect satisfies Logger interface

func (*DefaultSQLLogger) RowsClose added in v0.2.0

func (dl *DefaultSQLLogger) RowsClose(rowsID int64)

RowsClose satisfies Logger interface

func (*DefaultSQLLogger) StmtClose added in v0.2.0

func (dl *DefaultSQLLogger) StmtClose(stmtID int64)

StmtClose satisfies Logger interface

func (*DefaultSQLLogger) StmtExec added in v0.2.0

func (dl *DefaultSQLLogger) StmtExec(stmtID int64, query string, args []driver.Value)

StmtExec satisfies Logger interface

func (*DefaultSQLLogger) StmtExecContext added in v0.2.0

func (dl *DefaultSQLLogger) StmtExecContext(stmtID int64, query string, args []driver.NamedValue)

StmtExecContext satisfies Logger interface

func (*DefaultSQLLogger) StmtQuery added in v0.2.0

func (dl *DefaultSQLLogger) StmtQuery(stmtID, rowsID int64, query string, args []driver.Value)

StmtQuery satisfies Logger interface

func (*DefaultSQLLogger) StmtQueryContext added in v0.2.0

func (dl *DefaultSQLLogger) StmtQueryContext(stmtID int64, rowsID int64, query string, args []driver.NamedValue)

StmtQueryContext satisfies Logger interface

func (*DefaultSQLLogger) TxCommit added in v0.2.0

func (dl *DefaultSQLLogger) TxCommit(txID int64)

TxCommit satisfies Logger interface

func (*DefaultSQLLogger) TxRollback added in v0.2.0

func (dl *DefaultSQLLogger) TxRollback(txID int64)

TxRollback satisfies Logger interface

type SQLLogger added in v0.2.0

type SQLLogger interface {
	// Called on DB connect with a generated connection id
	Connect(connID int64)

	// Called on a transaction begin on a connection with the connection id and a generated transaction id
	ConnBegin(connID, txID int64, opts driver.TxOptions)
	// Called on a prepare statement on a connection with the connection id and a generated statement id
	ConnPrepare(connID, stmtID int64, query string)
	// Called on a prepare statement with context on a connection with the connection id and a generated statement id
	ConnPrepareContext(connID int64, stmtID int64, query string)
	// Called on a query on a connection with the connection id and a generated rows id
	ConnQuery(connID, rowsID int64, query string, args []driver.Value)
	// Called on a query with context on a connection with the connection id and a generated rows id
	ConnQueryContext(connID int64, rowsID int64, query string, args []driver.NamedValue)
	// Called on an exec on a connection with the connection id
	ConnExec(connID int64, query string, args []driver.Value)
	// Called on an exec with context on a connection with the connection id
	ConnExecContext(connID int64, query string, args []driver.NamedValue)
	// Called on a close on a connection with the connection id
	ConnClose(connID int64)

	// Called on an exec on a statement with the statement id
	StmtExec(stmtID int64, query string, args []driver.Value)
	// Called on an exec with context on a statement with the statement id
	StmtExecContext(stmtID int64, query string, args []driver.NamedValue)
	// Called on a query on a statement with the statement id and generated rows id
	StmtQuery(stmtID int64, rowsID int64, query string, args []driver.Value)
	// Called on a query with context on a statement with the statement id and generated rows id
	StmtQueryContext(stmtID int64, rowsID int64, query string, args []driver.NamedValue)
	// Called on a close on a statement with the statement id
	StmtClose(stmtID int64)

	// Called on a close on rows with the rows id
	RowsClose(rowsID int64)

	// Called on a commit on a transaction with the transaction id
	TxCommit(txID int64)
	// Called on a rollback on a transaction with the transaction id
	TxRollback(txID int64)
}

SQLLogger is the interface for a specialized SQL logger that is used to log SQL queries and operations in the LoggingConnector

With this interface, adapters can be implemented for any log framework. For the standard library log.Logger, a DefaultSQLLogger is provided as a default implementation.

All methods are only called if the original operation returned without an error.

type StdLogger added in v0.2.0

type StdLogger interface {
	Printf(format string, args ...interface{})
}

StdLogger is an interface to adapt the DefaultSQLLogger to the standard library log.Logger or other log frameworks

Directories

Path Synopsis
example module

Jump to

Keyboard shortcuts

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