如何在使用 JDBC 时为 SQL 语句启用日志记录
我正在尝试通过连接到eclipse IDE中的Oracle数据库来使用我的JDBC程序启用日志。
我已经通过这个SO post JDBC日志记录到文件,然后我已经创建了下面的java程序并从我的eclipse IDE运行它,但我无法看到JDBC驱动程序类生成的任何日志。
import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.LogManager;
import java.util.logging.Logger;
public class Logging {
static Logger log = Logger.getLogger(Logging.class.toString());
static Connection con = null;
public static void main(String[] args) throws SQLException,
ClassNotFoundException {
System.setProperty("oracle.jdbc.Trace", Boolean.TRUE.toString());
System.setProperty("java.util.logging.config.file",
"OracleLog.properties");
log.info("Test Message");
enableLogging(false);
getConnection();
closeConnection();
}
static private void enableLogging(boolean logDriver) {
try {
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));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException,
ClassNotFoundException {
Properties connectionProps = new Properties();
connectionProps.put("user", "test_app");
connectionProps.put("password", "test");
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(
"jdbc:oracle:thin:@"+HOST_IP+":1521:"+SID,
connectionProps);
System.out.println("Connected to database");
return con;
}
public static void closeConnection() throws SQLException {
if (con != null) {
con.close();
}
}
}
并且我的OracleLog.properties文件中有以下内容:
.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
但是当我通过放入类路径来运行我的程序时,我得到异常为:ojdbc6-11.2.0.3.jar
INFO: Test Message
javax.management.InstanceNotFoundException: com.oracle.jdbc:type=diagnosability,name=sun.misc.Launcher$AppClassLoader@73d16e93
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1095)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getAttribute(DefaultMBeanServerInterceptor.java:643)
at com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(JmxMBeanServer.java:678)
at myjdbc.Logging.enableLogging(Logging.java:45)
at myjdbc.Logging.main(Logging.java:24)
Connected to database
如果我在类路径中有,那么我也会得到同样的异常。ojdbc6_g.jar
请让我知道我如何为我的JDBC程序启用日志记录?基本上,我希望看到内部JDBC代码生成的日志。
更新:现在我把文件放在类路径中,我的程序给出了以下异常:ojdbc6dms.jar
Nov 28, 2014 9:09:02 PM jdbc.chap2.Logging main
INFO: Test Message
javax.management.InstanceNotFoundException: com.oracle.jdbc:type=diagnosability,name=sun.misc.Launcher$AppClassLoader@73d16e93
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1095)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getAttribute(DefaultMBeanServerInterceptor.java:643)
at com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(JmxMBeanServer.java:678)
at jdbc.chap2.Logging.enableLogging(Logging.java:45)
at jdbc.chap2.Logging.main(Logging.java:24)
Exception in thread "main" java.lang.NoClassDefFoundError: oracle/dms/console/DMSConsole
at oracle.jdbc.driver.DMSFactory.<clinit>(DMSFactory.java:48)
at oracle.jdbc.driver.PhysicalConnection.createDMSSensors(PhysicalConnection.java:2121)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:730)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:433)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:608)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:208)
at jdbc.chap2.Logging.getConnection(Logging.java:70)
at jdbc.chap2.Logging.main(Logging.java:25)
Caused by: java.lang.ClassNotFoundException: oracle.dms.console.DMSConsole
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 10 more