Is there a way to enable MySQL general query logging through JDBC? The closest thing I have found through my search is the ability to log slow queries through JDBC (http://dev.mysql.com/doc/refman/5.5/en/connector-j-reference-configuration-properties.html). Maybe I should do that and set the slow query threshold to 0 ms?
I would like to log all queries through MySQL in a human-readable format and would like to specify the location where the log file should be written. I know I will take a performance hit, but my application only has one user and is simple enough that I would be surprised if the performance hit was noticeable. I would like to try it out anyway to see.
I believe another option I have is to turn on binary logging and use mysqlbinlog to convert the binary logs to a human-readable format, but it sounds like the general query log would provide a simpler means of getting what I want.
Add 'logger' and 'profileSQL' to the jdbc url:
Then you will get the SQL statement below:
The default logger is:
Mysql jdbc property list: https://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html
I ended up finding a workaround. I enable MySQL general query logging through Java by modifying MySQL global system variables at runtime with the following SQL queries.
I recommend using forward slashes in the general_log_file path. I could not get backslashes to work, even in a Windows environment.
I disable general query logging at runtime with the following SQL query.
You can enable logging in the JDBC URL like this:
Other logging backends are available (CommonsLogger, Slf4jLogger, JDK14Logger). I believe direct Log4J logging was dropped at some point due to licencing issues so it might not work with your version of the JDBC driver.
Naturally, you'll need the relevant logging library's JAR in your classpath, and a configuration file (log4j.properties). I would set the root level to TRACE first to see what's happening and tighten it up by log level and category once you see what's being logged.
Further reading:
HTH
If you're using Hibernate, and perform all data access through it, you can turn on logging by setting the property
hibernate.show_sql
to true. This will write parameterized statements (e.g.SELECT foo.id FROM foo WHERE foo.bar = ?
), though. If you need parameter values, or don't use a tool like Hibernate, you might need to have MySQL write this log. See the MySQL documentation on the general query log.FWIW, the MySQL binary log is a means to a different end; it records changes to the data, and is used for incremental backups and/or replication.
SELECT
statements aren't recorded in the binary log.EDIT: I was able to get MySQL to write the general log by adding the following two lines to my.cnf (after confirming neither variable was already set), and restarting MySQL: