in DataSource.groovy (as per these instructions) was enough to get it working in my environment. It seems that parts of the FAQ are out of date (e.g. the many-to-many columns backwards question) so this might also be something that changed in the meantime.
I find it more useful to do the following, which is to enable Hibernate's logging to log the SQL along with bind variables (so you can see the values passed into your calls, and easily replicate the SQL in your editor or otherwise).
In your Config.groovy, add the following to your log4j block:
log4j = {
// Enable Hibernate SQL logging with param values
trace 'org.hibernate.type'
debug 'org.hibernate.SQL'
//the rest of your logging config
// ...
}
It avoids the performance problems of trace logging the Hibernate type package. This works with Hibernate 3.6 and above. I got this from: https://burtbeckwith.com/blog/?p=1604
All the answers above work and are correct. But they do not show the complete query in a nice human readable way. If want to see the final (without any ?, ?) query you have two options.
A) proxy your jdbc connection with log4jdbc or p6Spy.
B) look at it on database level. For example really easy to do with mysql.
Find out where you general_log_file is. Active general log if no activated already.
mysql command line> show variables like "%general_log%";
mysql command line> set global general_log = true;
Now everything is logged to you log file. Mac / linux example to show nice stream of your queries.
I know this was asked and answered long back .But I just happened to see this question and couldn't stop myself in answering or sharing our sql logging implementation approach in our project.
Hope it be of some help.
Currently it is in development environment.
We are using "log4jdbc Driver Spy " to log sql.
Configuration:
In your BuildConfig.groovy:
add below dependencies:
From my personal experience I found it quite useful and helpful while debugging.
Also more information you can find in this site. https://code.google.com/p/log4jdbc-remix/
Pure for reference only, but I use p6spy to log the SQL queries. It's a small intermediate jdbc driver. The exact query is logged as it would be send to the server (with parameters included).
include it in your project:
runtime 'p6spy:p6spy:3.0.0'
Change your datasource driver:
driverClassName: com.p6spy.engine.spy.P6SpyDriver
And your jdbc url:
url: jdbc:p6spy:mysql://
Configure it using spy.properties (in grails-app/conf).
This is a variation on many of the solutions above, but allows you to tweak the value at runtime. And just like the other solutions that deal with logToStdout it only shows the queries and not the bind values.
The idea was stolen from a burtbeckwith post I read some years ago that I can't find right now. It has been edited to work with grails 3.3.
A similar technique can be used to turn on logging for specific integration tests:
To better visualize SQL queries, add the following:
dataSource:
formatSql: true
If you want logback configuration for development only, you can add the following to the block environments > development with logging configuration in conf/logback-dev.xml: