sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

How to print the SQL log info?

Open yinhuanyi opened this issue 4 years ago • 8 comments

If we execute a sql, how can we print the raw SQL into my log file.

yinhuanyi avatar Nov 22 '21 07:11 yinhuanyi

me too

0x457 avatar Dec 31 '21 03:12 0x457

+1

shyandsy avatar Jan 12 '22 07:01 shyandsy

Would be nice to have an easy way to hook or wrap queries so we can catch and log long running queries at an application level

rowanseymour avatar Jan 27 '22 18:01 rowanseymour

+1

BlackSinny avatar Jul 25 '22 07:07 BlackSinny

@yinhuanyi I also have this case some time ago.

If you don't mind adding an external library, I wrote a transparent logger for standard library sql.DB which you can pass it to sqlx. (https://github.com/simukti/sqldb-logger)

Here is the sample integration with sqlx:

package main

import (
	"database/sql"
	"os"

	"github.com/jmoiron/sqlx"
	_ "github.com/mattn/go-sqlite3"
	"github.com/rs/zerolog"
	sqldblogger "github.com/simukti/sqldb-logger"
	"github.com/simukti/sqldb-logger/logadapter/zerologadapter"
)

func main() {
	// initiate *sql.DB
	driverName := "sqlite3"
	dsn := "file:sample.db"
	db, err := sql.Open(driverName, dsn)
	if err != nil {
		return
	}
	// initiate zerolog
	zerolog.SetGlobalLevel(zerolog.DebugLevel)
	zlogger := zerolog.New(os.Stdout).With().Timestamp().Logger()
	// prepare logger
	loggerOptions := []sqldblogger.Option{
		sqldblogger.WithSQLQueryFieldname("sql"),
		sqldblogger.WithWrapResult(false),
		sqldblogger.WithExecerLevel(sqldblogger.LevelDebug),
		sqldblogger.WithQueryerLevel(sqldblogger.LevelDebug),
		sqldblogger.WithPreparerLevel(sqldblogger.LevelDebug),
	}
	// wrap *sql.DB to transparent logger
	db = sqldblogger.OpenDriver(dsn, db.Driver(), zerologadapter.New(zlogger), loggerOptions...)
	// pass it sqlx
	sqlxDB := sqlx.NewDb(db, driverName)
	// use sqlxDB as usual, no need any more change
	_ = sqlxDB.Ping()
	var res int
	_ = sqlxDB.QueryRowx(`SELECT 1+2`).Scan(&res)
}

Log output:

{"level":"debug","conn_id":"G6Oyg4iqK6SOO6au","duration":1.057364,"time":1660201999,"time":"2022-08-11T14:13:19+07:00","message":"Connect"}
{"level":"debug","conn_id":"G6Oyg4iqK6SOO6au","duration":0.000671,"time":1660201999,"time":"2022-08-11T14:13:19+07:00","message":"Ping"}
{"level":"debug","conn_id":"G6Oyg4iqK6SOO6au","duration":0.026285,"sql":"SELECT 1+2","time":1660201999,"time":"2022-08-11T14:13:19+07:00","message":"QueryContext"}

If you want to output the logs to a file, pass a file writer to the logger.

I hope that helps.

simukti avatar Aug 11 '22 07:08 simukti

I found another library sql for hook: https://github.com/qustavo/sqlhooks they can do before & after for the query executed/queried

kecci avatar Oct 13 '22 09:10 kecci

+1

Bjohnson131 avatar Aug 14 '23 05:08 Bjohnson131

but i can not print real code line, i see gorm had been realized yet

BlackSinny avatar Aug 17 '23 02:08 BlackSinny