-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change duration to time.Duration in LoggerFunc
- Loading branch information
Showing
3 changed files
with
18 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
|
@@ -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) | ||
} | ||
}() | ||
|
||
|
@@ -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) | ||
} | ||
}() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters