JDBC 记录到文件 [英] JDBC logging to file

查看:28
本文介绍了JDBC 记录到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将项目中 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.

我查看了 SLF4Jjdbcdslog,但我不确定如何使用它登录到文件.此外,我需要过滤"一些日志(因为我不需要知道何时调用了某些 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.

谢谢.

** 更新 **

我找到了这篇 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

声明要由 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.

祝您登录愉快!

这篇关于JDBC 记录到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆