Skip to content

Commit

Permalink
change duration to time.Duration in LoggerFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
lqs committed Jul 23, 2024
1 parent 252be53 commit ef4e632
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
22 changes: 10 additions & 12 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
// 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/[email protected]/database.go
Expand All @@ -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 += ";"
Expand All @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -212,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)
}
}()

Expand Down Expand Up @@ -250,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)
}
}()

Expand Down
3 changes: 2 additions & 1 deletion database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"database/sql/driver"
"errors"
"testing"
"time"
)

func (m *mockConn) Prepare(query string) (driver.Stmt, error) {
Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 6 additions & 6 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}()

Expand Down Expand Up @@ -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)
}
}()

Expand Down

0 comments on commit ef4e632

Please sign in to comment.