Is there a way to get the sql query log from https://github.com/jinzhu/gorm?
e.g. in dev environment, it would be useful to be able to log to the console the mysql queries that have been called.
e.g. how to get the underlying sql query log for the following queries:
gorm.Find(&todos)
gorm.Preload("User").Find(&todos)
I am aware that I can call:
gorm.Debug().Find(&todos)
gorm.Debug().Preload("User").Find(&todos)
but I would like to only call Debug()
if in dev envrionment and not in production
In the new version (GORM v2), use the Logger
interface:
import "gorm.io/gorm/logger"
db, err := gorm.Open(mysql.Open(connectionDSN), &gorm.Config{
Logger: logger.Default.LogMode(logger.Info),
})
For old version (GORM v1):
db, err:= Open(dbType, connectionDSN);
db.LogMode(true)
Note: this is not specific to MySQL and will work with any other DB driver (e.g. Postgres, SQLite, etc.).