JDBC记录到文件 [英] JDBC logging to file
问题描述
我需要将项目中Oracle数据库的所有查询记录到日志文件中。
I need to log all the queries to an Oracle database in my project to a log file.
什么是好的解决方案为达到这个?一些样本用法将不胜感激。
What would be a good solution to achieve this? Some sample usage would be appreciated.
我查看了 SLF4J 使用 jdbcdslog ,但我不确定如何使用 jdbcdslog 登录文件它。此外,我需要过滤一些日志(因为我不需要知道何时调用一些 getxxxx
方法)
I have looked at SLF4J with jdbcdslog, but I'm not sure how I can log to a file with it. Moreover, I would need to "filter" some of the logs (because I don't need to know when some getxxxx
method get's invoked)
最好,我更喜欢使用 java.util.logging
,但这不是必需的。
Preferably, I'd prefer to use java.util.logging
but it is not a requirement.
谢谢。
** 更新 **
I找到了这篇Oracle文章,但它没有真的告诉我如何以编程方式做同样的事情。
I found this Oracle article, however it does not really tell how to programatically do the same thing.
推荐答案
经过多次阅读,这就是我的工作方式:
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
声明 ConsoleHandler
供Oracle的JDBC驱动程序使用。可以在此处声明任意数量和任意数量的处理程序,每行一个,具有类的完全限定名称:
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
将调用<$ c的方法 setLevel(Level.INFO)
$ c> ConsoleHandler 处理程序实例。
will call the methode setLevel(Level.INFO)
of the ConsoleHandler
handler instance.
com.my.own.project.logging.handler.MyHandler.foo=Bar
将调用方法 setFoo(Bar)
。就是这样。 MyHandler
处理程序实例的
will call the method setFoo("Bar")
of the MyHandler
handler instance. And that's it.
快乐的伐木!
这篇关于JDBC记录到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!