https://github.com/networkteam/go-sqllogger
Go SQL driver adapter for extended logging
https://github.com/networkteam/go-sqllogger
Last synced: about 2 months ago
JSON representation
Go SQL driver adapter for extended logging
- Host: GitHub
- URL: https://github.com/networkteam/go-sqllogger
- Owner: networkteam
- License: mit
- Created: 2019-08-26T14:42:51.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-05-14T10:34:24.000Z (11 months ago)
- Last Synced: 2025-08-13T23:17:53.927Z (8 months ago)
- Language: Go
- Size: 44.9 KB
- Stars: 25
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-sqllogger
[](https://goreportcard.com/report/github.com/networkteam/go-sqllogger)
[](http://godoc.org/github.com/networkteam/go-sqllogger)
[](https://github.com/networkteam/go-sqllogger/releases)
[](https://travis-ci.com/networkteam/go-sqllogger)
**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](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](./example/main.go):
```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](./LICENSE)