Add automatic query logger function with additional execution details.

This commit is contained in:
go-jet 2022-01-12 19:03:50 +01:00
parent 7377e078cd
commit 4955bfc4b5
18 changed files with 266 additions and 59 deletions

View file

@ -8,6 +8,7 @@ import (
"github.com/go-jet/jet/v2/tests/dbconfig"
"github.com/stretchr/testify/require"
"math/rand"
"runtime"
"time"
_ "github.com/go-sql-driver/mysql"
@ -51,11 +52,21 @@ var loggedSQL string
var loggedSQLArgs []interface{}
var loggedDebugSQL string
var queryInfo jetmysql.QueryInfo
var callerFile string
var callerLine int
var callerFunction string
func init() {
jetmysql.SetLogger(func(ctx context.Context, statement jetmysql.PrintableStatement) {
loggedSQL, loggedSQLArgs = statement.Sql()
loggedDebugSQL = statement.DebugSql()
})
jetmysql.SetQueryLoggerFunc(func(ctx context.Context, info jetmysql.QueryInfo) {
queryInfo = info
callerFile, callerLine, callerFunction = info.Caller()
})
}
func requireLogged(t *testing.T, statement postgres.Statement) {
@ -65,6 +76,21 @@ func requireLogged(t *testing.T, statement postgres.Statement) {
require.Equal(t, loggedDebugSQL, statement.DebugSql())
}
func requireQueryLogged(t *testing.T, statement postgres.Statement, rowsProcessed int64) {
query, args := statement.Sql()
queryLogged, argsLogged := queryInfo.Statement.Sql()
require.Equal(t, query, queryLogged)
require.Equal(t, args, argsLogged)
require.Equal(t, queryInfo.RowsProcessed, rowsProcessed)
pc, file, _, _ := runtime.Caller(1)
funcDetails := runtime.FuncForPC(pc)
require.Equal(t, file, callerFile)
require.NotEmpty(t, callerLine)
require.Equal(t, funcDetails.Name(), callerFunction)
}
func skipForMariaDB(t *testing.T) {
if sourceIsMariaDB() {
t.SkipNow()