Logging using java.util.logging

The PostgreSQL® JDBC Driver supports the use of logging (or tracing) to help resolve issues with thepgJDBC Driver when is used in your application.

The pgJDBC Driver uses the logging APIs ofjava.util.logging that is part of Java since JDK 1.4, which makes it a goodchoice for the driver since it doesn’t add any external dependency for a logging framework.java.util.logging is a veryrich and powerful tool, it’s beyond the scope of these docs to explain how to use it to it’s full potential, for thatplease refer toJava Logging Overview.

This logging support was added since version 42.0.0 of the pgJDBC Driver, and previous versions uses a custom mechanismto enable logging that it is replaced by the use ofjava.util.logging in current versions, the old mechanism is no longer available.

NOTE

Please note that while most people asked the use of a Logging Framework for a long time, this support is mainly todebug the driver itself and not for general SQL query debug.

Configuration

The Logging APIs offer both static and dynamic configuration control. Static control enables field service staff to setup a particular configuration and then re-launch the application with the new logging settings. Dynamic control allowsfor updates to the logging configuration within a currently running program.

The root logger used by the pgJDBC driver isorg.postgresql .

Enable logging by using logging.properties file

The default Java logging framework stores its configuration in a file calledlogging.properties . Settings are storedper line using a dot notation format. Java installs a global configuration file in thelib folder of the Java installationdirectory, although you can use a separate configuration file by specifying thejava.util.logging.config.file propertywhen starting a Java program.logging.properties files can also be created and stored with individual projects.

The following is an example of setting that you can make in thelogging.properties :

# 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/pgjdbc%u.logjava.util.logging.FileHandler.limit=5000000java.util.logging.FileHandler.count=20java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatterjava.util.logging.FileHandler.level=FINESTjava.util.logging.SimpleFormatter.format=%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s %2$s %5$s%6$s%n# Facility specific properties.org.postgresql.level=FINEST

And when you run your application you pass the system property:

java -jar -Djava.util.logging.config.file=logging.properties run.jar

Connection Pools and Data Sources
Further Reading
Logging using java.util.logging