更新时间:2023-02-07 18:08:50
经过大量阅读后,我是这样开始工作的:
After much reading, this is how I got things working :
注意:有关详细信息,请阅读 Oracle JDBC 中的可诊断性 文档
NOTE : Fore more information, read the Oracle Diagnosability in JDBC document
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);
这就是魔法:
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));
}
}
属性文件(来自 Oracle 的文档):
The properties file (from Oracle's documentation) :
.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 handlers are declared
oracle.jdbc.handlers=java.util.logging.ConsoleHandler
声明要由 Oracle 的 JDBC 驱动程序使用的 ConsoleHandler
.可以在此处声明任意数量的处理程序,每行一个,带有类的全限定名:
Declares the ConsoleHandler
to be used by Oracle's JDBC driver. Any and any number of handlers can be declared here, one per line, with the class' full qualified name :
oracle.jdbc.handlers=java.util.logging.ConsoleHandler
oracle.jdbc.handlers=java.util.logging.FileHandler
...
可以提供自己定制的具有相同规则的处理程序.以下几行用于设置处理程序
One can provide their own custom made handlers with the same rule. The following lines are to setup the handler
java.util.logging.ConsoleHandler.level=INFO
将调用ConsoleHandler
处理程序实例的方法setLevel(Level.INFO)
.
will call the methode setLevel(Level.INFO)
of the ConsoleHandler
handler instance.
com.my.own.project.logging.handler.MyHandler.foo=Bar
将调用 MyHandler
处理程序实例的方法 setFoo("Bar")
.就是这样.
will call the method setFoo("Bar")
of the MyHandler
handler instance. And that's it.
祝您登录愉快!