Search This Blog

Pages

Tuesday, July 19, 2016

How to enable FINEST logging of SQL Server JDBC Driver

Add following code snippet in constructor of your class that serves as interface to the SQL Server JDBC driver which is typically where you pass the connection credentials of your SQL Server database.
===============
        java.util.logging.Logger jlogger = java.util.logging.Logger.getLogger("com.microsoft.sqlserver.jdbc");
        java.util.logging.FileHandler jfh;

        try
        {
            // This block configure the logger with handler and formatter
            jfh = new java.util.logging.FileHandler("C:/temp/java-sqljdbc-driver.log");
            jlogger.addHandler(jfh);
            java.util.logging.SimpleFormatter jformatter = new java.util.logging.SimpleFormatter();
            jfh.setFormatter(jformatter);
            jlogger.setLevel(java.util.logging.Level.FINEST);

            // the following statement is used to log any messages
            jlogger.info("MY FIRST TEST LOG MESSAGE");
        }
        catch (SecurityException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
===============

In above example the logging level is set to FINEST, but it can be set to FINE and FINER as well. On execution of your test program FINE level logging to file "C:/temp/java-sqljdbc-driver.log" is enabled. This file comes handy for debugging issues due to bugs in JDBC driver.