Enabling MySQL general query log with JDBC

后端 未结 5 1282
傲寒
傲寒 2020-12-06 05:26

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://d

5条回答
  •  一整个雨季
    2020-12-06 05:54

    You can enable logging in the JDBC URL like this:

    jdbc:mysql://host/db?logger=com.mysql.jdbc.log.Log4JLogger&profileSQL=true
    

    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:

    • https://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html
    • http://docs.oracle.com/cd/E17952_01/mysql-monitor-2.3-en/mem-qanal-using-cj.html

    HTH

提交回复
热议问题