golang gorm访问底层的mysql查询 [英] golang gorm Access the underlying mysql query

查看:52
本文介绍了golang gorm访问底层的mysql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以从 https://github.com/jinzhu/gorm

Is there a way to get the sql query log from https://github.com/jinzhu/gorm?

例如在开发环境中,能够将已调用的mysql查询登录到控制台会很有用.

e.g. in dev environment, it would be useful to be able to log to the console the mysql queries that have been called.

例如如何获取以下查询的基础sql查询日志:

e.g. how to get the underlying sql query log for the following queries:

gorm.Find(&todos)
gorm.Preload("User").Find(&todos)

我知道我可以打电话给

gorm.Debug().Find(&todos)
gorm.Debug().Preload("User").Find(&todos)

但是我只想在开发环境而不是生产环境中调用Debug()

but I would like to only call Debug() if in dev envrionment and not in production

推荐答案

这可以解决问题:

db, err:= Open(dbType, connectionDSN);
db.LogMode(true)

这篇关于golang gorm访问底层的mysql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆