New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use interface to specify the logger #4

Merged
merged 2 commits into from May 18, 2015
Jump to file or symbol
Failed to load files and symbols.
+19 −10
Diff settings

Always

Just for now

View
@@ -3,20 +3,26 @@ package proxy
import (
"database/sql/driver"
"fmt"
"log"
"time"
)
// Outputter is what is used by the tracing proxy created via `NewTraceProxy`.
// Anything that implements a `log.Logger` style `Output` method will satisfy
// this interface.
type Outputter interface{
Output(calldepth int, s string) error
}
// NewTraceProxy generates a proxy that logs queries.
func NewTraceProxy(d driver.Driver, logger *log.Logger) *Proxy {
func NewTraceProxy(d driver.Driver, o Outputter) *Proxy {
return &Proxy{
Driver: d,
Hooks: &Hooks{
PreOpen: func(_ string) (interface{}, error) {
return time.Now(), nil
},
PostOpen: func(ctx interface{}, _ driver.Conn) error {
logger.Output(
o.Output(
7,
fmt.Sprintf(
"Open (%s)",
@@ -29,7 +35,7 @@ func NewTraceProxy(d driver.Driver, logger *log.Logger) *Proxy {
return time.Now(), nil
},
PostExec: func(ctx interface{}, stmt *Stmt, args []driver.Value, _ driver.Result) error {
logger.Output(
o.Output(
7,
fmt.Sprintf(
"Exec: %s; args = %v (%s)",
@@ -44,7 +50,7 @@ func NewTraceProxy(d driver.Driver, logger *log.Logger) *Proxy {
return time.Now(), nil
},
PostQuery: func(ctx interface{}, stmt *Stmt, args []driver.Value, _ driver.Rows) error {
logger.Output(
o.Output(
9,
fmt.Sprintf(
"Query: %s; args = %v (%s)",
@@ -56,15 +62,15 @@ func NewTraceProxy(d driver.Driver, logger *log.Logger) *Proxy {
return nil
},
Begin: func(conn *Conn) error {
logger.Output(6, "Begin")
o.Output(6, "Begin")
return nil
},
Commit: func(tx *Tx) error {
logger.Output(6, "Commit")
o.Output(6, "Commit")
return nil
},
Rollback: func(tx *Tx) error {
logger.Output(8, "Rollback")
o.Output(8, "Rollback")
return nil
},
},
View
@@ -51,8 +51,11 @@ func TestTraceProxy(t *testing.T) {
// Fake time compinent with (\d+\.\d+[^\)]+)
regexp.MustCompile(`tracer_test.go:27: Open \(\d+\.\d+[^\)]+\)`),
regexp.MustCompile(`tracer_test.go:27: Exec: CREATE TABLE t1 \(id INTEGER PRIMARY KEY\); args = \[\] \(\d+\.\d+[^\)]+\)`),
regexp.MustCompile(`txmanager.go:177: Begin`),
regexp.MustCompile(`txmanager.go:182: Exec: INSERT INTO t1 \(id\) VALUES\(\?\); args = \[1\] \(\d+\.\d+[^\)]+\)`),
// The line numbers on these two lines may change depending on the version
// of txmanager that you have. For now, we cross our fingers that the
// filename doesn't change ever, but the line numbers may change.
regexp.MustCompile(`txmanager.go:\d+: Begin`),
regexp.MustCompile(`txmanager.go:\d+: Exec: INSERT INTO t1 \(id\) VALUES\(\?\); args = \[1\] \(\d+\.\d+[^\)]+\)`),
regexp.MustCompile(`tracer_test.go:36: Commit`),
regexp.MustCompile(`tracer_test.go:41: Query: SELECT id FROM t1 WHERE id = \?; args = \[1\] \(\d+\.\d+[^\)]+\)`),
}
ProTip! Use n and p to navigate between commits in a pull request.