Easy JDBC Logging

I have been supporting Microsoft’s JDBC driver for almost six years now and the one thing with which I always struggle is getting logging going.  JDBC logging is probably some of the most useful logging out there (I only wish BID tracing were so easy to enable and consume!), but for some reason I always struggle getting the correct logging.properties file registered and then figuring out exactly where the log file will be generated.  I finally got tired of fighting with it and decided to change both my test code and my command-line to make this much, much easier.

The first thing to recognize is that Java will by default generate the log file in the User.Home folder.  Therefore, I decided to output that location as part of my code:

 System.out.println("User Home Path: " + System.getProperty("user.home"));

The second thing to do is to manually specify the logging.properties file in the command-line:

 java.exe -Djava.util.logging.config.file=c:\temp\logging.properties myJavaClass

Just in case you were wondering, I am using a very simple logging.properties file:

 # Specify the handlers to create in the root logger
 # (all loggers are children of the root logger)
 # The following creates two handlers
 handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler
  
 # Set the default logging level for the root logger
 .level = ALL
  
 # Set the default logging level for new ConsoleHandler instances
 java.util.logging.ConsoleHandler.level = INFO
  
 # Set the default logging level for new FileHandler instances
 java.util.logging.FileHandler.level = ALL
  
 # Set the default formatter for new ConsoleHandler instances
 java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
  
  
 ############################################################
 # Facility specific properties.
 # Provides extra control for each logger.
 ############################################################
  
 # For example, set the com.xyz.foo logger to only log SEVERE
 # messages:
 com.microsoft.sqlserver.jdbc.level=FINEST
 com.xyz.foo.level = SEVERE

Now, for the one of the few times where I have needed to generate a JDBC log, it happens on the first time!

Untitled

Happy logging!