golang gorm Access the underlying mysql query

前端 未结 2 856
无人共我
无人共我 2020-12-29 02:45

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 q

相关标签:
2条回答
  • 2020-12-29 03:22

    You can pass your own logger to gorm using gorm.SetLogger method. It uses the Print method of the logger to print logs as well as SQL queries. The log level of Print method for any logger(logrus/go's inbuild logger) is generally set to INFO. While passing the logger to gorm, if you set the log level to anything below or equal to INFO(DEBUG/INFO) you can see the sql queries and other logs by gorm

    Also you can parse the log level from a config file where you can set it based on environment

    0 讨论(0)
  • 2020-12-29 03:28

    This will do the trick:

    db, err:= Open(dbType, connectionDSN);
    db.LogMode(true)
    
    0 讨论(0)
提交回复
热议问题