dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


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.
    @Override
    public void write(char[] cbuf, int off, int len) throws IOException
    {
        myLogger.log(myLogLevel, new String(cbuf, off, len));
    }

    @Override
    public void flush() throws IOException
    {
    }

    @Override
    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(
            Logger.getLogger("database"))));
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    }
}
Mahendar Aleti
 
Comment and Contribute

 

 

 

 

 


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

 

 

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