Monday, February 11, 2008

Oracle JDBC Logging

select the appropriate jar for logging
ojdbc14_g.jar
ojdbc5_g.jar
ojdbc6_g.jar
ojdbc5dms.jar minimal logging
ojdbc6dms.jar minimal logging
ojdbc5dms_g.jar
ojdbc6dms_g.jar

put it on the classpath and make sure it's the only one.

Using System properties

Create a properties file to set the different levels of tracing, for example:

handlers=java.util.logging.ConsoleHandler,java.util.logging.FileHandler

# default file output is in user's home directory.

java.util.logging.FileHandler.pattern = jdbc.log
java.util.logging.FileHandler.limit = 50000

java.util.logging.FileHandler.count = 1

java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter


# Predefined levels are:

# ALL, SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, OFF
# Setting this to SEVERE avoids duplicate output from default logger

java.util.logging.ConsoleHandler.level = SEVERE

java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

oracle.jdbc.level = FINEST

To run the test with tracing enabled use following command (have trace enable jars *_g.jar in class path instead of optimized jdbc jars):

java -Doracle.jdbc.Trace=true -Djava.util.logging.config.file=properties file location program

Using java.util.logging

import oracle.jdbc.driver.OracleLog;
// set the file handler

FileHandler handler = new FileHandler("oracle.log");

// designate the module to be traced

Logger logger = Logger.getLogger("oracle.jdbc");

// set the tracing level

logger.setLevel(Level.FINE);

// Add file handler to the desired logger

logger.addHandler(handler);

// turn on trace

OracleLog.setTrace(true);


// java codef

// turn off trace

OracleLog.setTrace(false);

Programmatically

// compute the ObjectName

String loader= Thread.currentThread().getContextClassLoader().toString().replaceAll("[,=:\"]+", "");

javax.management.ObjectName name= new javax.management.ObjectName("com.oracle.jdbc:type=diagnosability,name="+loader);

// get the MBean server

javax.management.MBeanServer mbs= java.lang.management.ManagementFactory.getPlatformMBeanServer();

// find out if logging is enabled or not

System.out.println("LoggingEnabled = " +mbs.getAttribute(name, "LoggingEnabled"));

// enable logging

mbs.setAttribute(name,new javax.management.Attribute("LoggingEnabled", true));

// disable logging

mbs.setAttribute(name,new javax.management.Attribute("LoggingEnabled", false));