Java – JDBC logging to file
I need to log all queries to the Oracle database in my project to a log file
What is a good way to achieve this? Some samples will be appreciated
I've seen slf4j and JDBC dslog, but I don't know how I can log in to a file In addition, I need to "filter" some logs (because I don't need to know when the getxxxx method is called)
Best of all, I prefer to use Java util. Logging, but this is not required
thank you.
**Renew**
I found this Oracle article, but it doesn't really tell me how to programmatically do the same thing
Solution
After a lot of reading, this is how I work:
Note: for more information, read the Oracle diagnostics in JDBC documentation
Properties prop = new Properties(); prop.put ("user",USER); prop.put ("password",PASS); // prop.put(propname,propValue); Class.forName("oracle.jdbc.driver.OracleDriver"); enableLogging(false); conn = DriverManager.getConnection("jdbc:oracle:thin:@"+HOST+":"+PORT+":"+SID,prop);
This is magic:
static private void enableLogging(boolean logDriver) throws MalformedObjectNameException,NullPointerException,AttributeNotFoundException,InstanceNotFoundException,MBeanException,ReflectionException,InvalidAttributeValueException,SecurityException,FileNotFoundException,IOException { oracle.jdbc.driver.OracleLog.setTrace(true); // 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)); File propFile = new File("path/to/properties"); LogManager logManager = LogManager.getLogManager(); logManager.readConfiguration(new FileInputStream(propFile)); if (logDriver) { DriverManager.setLogWriter(new PrintWriter(System.err)); } }
Properties file (from Oracle documents):
.level=SEVERE oracle.jdbc.level=INFO oracle.jdbc.handlers=java.util.logging.ConsoleHandler java.util.logging.ConsoleHandler.level=INFO java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
Basically, this is where the handler is declared
oracle.jdbc.handlers=java.util.logging.ConsoleHandler
Declare that the consolehandler is used by Oracle's jdbc driver Any and any number of handlers can be declared here, one for each line, and the fully qualified name of the class:
oracle.jdbc.handlers=java.util.logging.ConsoleHandler oracle.jdbc.handlers=java.util.logging.FileHandler ...
You can use the same rules to provide your own custom handlers The following lines are the settings handler
java.util.logging.ConsoleHandler.level=INFO
The method setlevel (level. Info) of the consolehandler handler instance will be called
com.my.own.project.logging.handler.MyHandler.foo=Bar
The method setfoo ("bar") of the myhandler handler instance will be called this is it
Happy login!