Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: Intermediate
Sep 26, 2006



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Using the Logger to Trace JDBC Driver Managers and Drivers

Both JDBC drivers and the DriverManager use the PrintWriter for logging/tracing. Using java.util.Logger as your logging/tracing tool allows you to take advantage of the Logging framework.

Create an adapter class to redirect the log messages to java.util.Logging.

// Adapter class to redirect the logging/tracing statements from the 
// PrintWriter to the Logger.
public class JDBCLogWriter extends Writer
    private final Logger myLogger;
    private final Level myLogLevel;

    public JDBCLogWriter(Logger logger)
        this(logger, Level.INFO);

    // Initialize the log writer
    public JDBCLogWriter(Logger logger, Level level)
        myLogger = logger;
        myLogLevel = level;

    // Redirect the log to the logger.
    public void write(char[] cbuf, int off, int len) throws IOException
        myLogger.log(myLogLevel, new String(cbuf, off, len));

    public void flush() throws IOException

    public void close() throws IOException

    public static void main(String[] args) throws Exception
        // Set the log writer by using the JDBCLogWriter
        DriverManager.setLogWriter(new PrintWriter(new JDBCLogWriter(
Mahendar Aleti
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date