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!
Happy logging!
Comments
Anonymous
October 18, 2011
The comment has been removedAnonymous
August 29, 2014
Hello Evan, Thanks for this useful info! i am trying to trace a particular logger and i did not work. I tried the following. com.microsoft.sqlserver.jdbc.Connection=FINE com.microsoft.sqlserver.jdbc.Connection.level=FINE com.microsoft.sqlserver.jdbc.Connection.Level=FINE com.microsoft.sqlserver.jdbc.Connection.LEVEL=FINEAnonymous
August 29, 2014
I think there was a problem with the root level logger. after correcting it then everything worked. below settings worked fine as expected. com.microsoft.sqlserver.jdbc.internals.level=OFF com.microsoft.sqlserver.jdbc.Connection.level=FINEST com.microsoft.sqlserver.jdbc.ResultSet.level=FINE com.microsoft.sqlserver.jdbc.internals.SQLServerStatement.level=FINE