Skip to content

JDBC Logging

Brett Wooldridge edited this page Oct 22, 2015 · 3 revisions

HikariCP does not inherently include JDBC logging at this time. This is a conscious decision, not an oversight or undeveloped future roadmap item. Nearly all major database have a JDBC driver capable of logging on its own. For those that do not, log4jdbc-log4j2 is a good option.

This wiki page documents how to enable logging for common databases, as well as log4jdbc-log4j2.

Using log4jdbc-log4j2

The name of this project is slightly misleading, as it also supports SLF4J, which implicitly supports every major logging framework.

You can use this method if your driver does not support logging directly, or if you simply prefer the flexibility of this solution over the database vendor's offering.

First, you will need the correct dependency:

<dependency>
  <groupId>org.bgee.log4jdbc-log4j2</groupId>
  <artifactId>log4jdbc-log4j2-jdbcXX</artifactId>
  <version>1.16</version>
</dependency>

Where "jdbcXX" is one of: jdbc3, jdbc4, or jdbc4.1.

Next, you will need to configure HikariCP. The simplest way to use log4jdbc-log4j2 is via their JDBC connection URL. In their scheme, you simply prepend jdbc:log4jdbc to your existing JDBC URL, like so:

jdbc:log4jdbc:jdbc:postgresql://localhost/simpsons

Additionally, and optionally, you can set the driver class to net.sf.log4jdbc.sql.jdbcapi.DriverSpy. A HikariCP configuration might look something like:

jdbcUrl=jdbc:log4jdbc:jdbc:postgresql://localhost/simpsons
driverClassName=net.sf.log4jdbc.sql.jdbcapi.DriverSpy

See the log4jdbc-log4j2 project page for detailed documentation about logging configuration.


Direct Logging Support

These sections detail how to enable logging provided natively by various drivers.

MySQL Connector/J

Example:

jdbcUrl=jdbc:mysql://localhost:3306/simpsons
dataSource.logger=com.mysql.jdbc.log.StandardLogger
dataSource.logSlowQueries=true
dataSource.dumpQueriesOnException=true

A full list of properties is available in the Debugging/Profiling section of the MySQL documentation.

Microsoft SQL Server

Logging for the SQL Server JDBC driver is controlled entirely by logging configuration, and no special configuration of the driver is required to enable it. The documentation on this webpage covers all of the logging options available.

This is an example of enabling logging using java.util.logging, but can be extrapolated for other logging frameworks:

# Specify the handler, the handlers will be installed during VM startup.
handlers= java.util.logging.FileHandler

# Default global logging level.
.level= OFF

# default file output is in user's home directory.
java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 5000000
java.util.logging.FileHandler.count = 20
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.level = FINEST

# Facility specific properties.
com.microsoft.sqlserver.jdbc.SQLServerStatement.level=FINE