Compare commits

...

2 Commits

Author SHA1 Message Date
Qishuai Liu ef4e632a9d
change duration to time.Duration in LoggerFunc 2024-07-23 23:42:12 +09:00
Qishuai Liu 252be5342f
don't enable default logger by default 2024-07-23 23:33:42 +09:00
3 changed files with 19 additions and 21 deletions

View File

@ -81,15 +81,15 @@ type database struct {
interceptor InterceptorFunc
}
type LoggerFunc func(sql string, durationNano int64, isTx bool, retry bool)
type LoggerFunc func(sql string, duration time.Duration, isTx bool, retry bool)
func (d *database) SetLogger(loggerFunc LoggerFunc) {
d.logger = loggerFunc
}
// defaultLogger is sqlingo default logger,
// DefaultLogger is sqlingo default logger,
// which print log to stderr and regard executing time gt 100ms as slow sql.
func defaultLogger(sql string, durationNano int64, isTx bool, retry bool) {
func DefaultLogger(sql string, duration time.Duration, isTx bool, retry bool) {
// for finding code position, try once is enough
once.Do(func() {
// $GOPATH/pkg/mod/github.com/lqs/sqlingo@vX.X.X/database.go
@ -111,8 +111,6 @@ func defaultLogger(sql string, durationNano int64, isTx bool, retry bool) {
}
}
// convert durationNano (int64) to time.Duration
du := time.Duration(durationNano)
// todo shouldn't append ';' here
if !strings.HasSuffix(sql, ";") {
sql += ";"
@ -121,7 +119,7 @@ func defaultLogger(sql string, durationNano int64, isTx bool, retry bool) {
sb := strings.Builder{}
sb.Grow(32)
sb.WriteString("|")
sb.WriteString(du.String())
sb.WriteString(duration.String())
if isTx {
sb.WriteString("|transaction") // todo using something traceable
}
@ -141,7 +139,7 @@ func defaultLogger(sql string, durationNano int64, isTx bool, retry bool) {
// print to stderr
fmt.Fprintln(os.Stderr, blue+line1+reset)
if du < 100*time.Millisecond {
if duration < 100*time.Millisecond {
fmt.Fprintf(os.Stderr, "%s%s%s\n", green, sql, reset)
} else {
fmt.Fprintf(os.Stderr, "%s%s%s\n", red, sql, reset)
@ -173,7 +171,6 @@ func Open(driverName string, dataSourceName string) (db Database, err error) {
}
}
db = Use(driverName, sqlDB)
db.SetLogger(defaultLogger)
return
}
@ -213,11 +210,11 @@ func (d database) queryContextOnce(ctx context.Context, sqlString string, retry
if ctx == nil {
ctx = context.Background()
}
startTime := time.Now().UnixNano()
startTime := time.Now()
defer func() {
endTime := time.Now().UnixNano()
endTime := time.Now()
if d.logger != nil {
d.logger(sqlString, endTime-startTime, false, retry)
d.logger(sqlString, endTime.Sub(startTime), false, retry)
}
}()
@ -251,11 +248,11 @@ func (d database) ExecuteContext(ctx context.Context, sqlString string) (sql.Res
ctx = context.Background()
}
sqlStringWithCallerInfo := getCallerInfo(d, false) + sqlString
startTime := time.Now().UnixNano()
startTime := time.Now()
defer func() {
endTime := time.Now().UnixNano()
endTime := time.Now()
if d.logger != nil {
d.logger(sqlStringWithCallerInfo, endTime-startTime, false, false)
d.logger(sqlStringWithCallerInfo, endTime.Sub(startTime), false, false)
}
}()

View File

@ -6,6 +6,7 @@ import (
"database/sql/driver"
"errors"
"testing"
"time"
)
func (m *mockConn) Prepare(query string) (driver.Stmt, error) {
@ -72,7 +73,7 @@ func TestDatabase(t *testing.T) {
interceptorExecutedCount++
return invoker(ctx, sql)
})
db.SetLogger(func(sql string, durationNano int64, _, _ bool) {
db.SetLogger(func(sql string, _ time.Duration, _, _ bool) {
if sql != "SELECT 1" {
t.Error(sql)
}

View File

@ -129,11 +129,11 @@ func (t transaction) queryContextOnce(ctx context.Context, sqlStringWithCallerIn
if ctx == nil {
ctx = context.Background()
}
startTime := time.Now().UnixNano()
startTime := time.Now()
defer func() {
endTime := time.Now().UnixNano()
endTime := time.Now()
if t.logger != nil {
t.logger(sqlStringWithCallerInfo, endTime-startTime, true, false)
t.logger(sqlStringWithCallerInfo, endTime.Sub(startTime), true, false)
}
}()
@ -165,11 +165,11 @@ func (t transaction) ExecuteContext(ctx context.Context, sqlString string) (sql.
ctx = context.Background()
}
sqlStringWithCallerInfo := getTxCallerInfo(t, false) + sqlString
startTime := time.Now().UnixNano()
startTime := time.Now()
defer func() {
endTime := time.Now().UnixNano()
endTime := time.Now()
if t.logger != nil {
t.logger(sqlStringWithCallerInfo, endTime-startTime, true, false)
t.logger(sqlStringWithCallerInfo, endTime.Sub(startTime), true, false)
}
}()