用spring为org.apache.commons.dbcp.BasicDataSource做日志连接池[英] logging connection pooling for org.apache.commons.dbcp.BasicDataSource with spring

本文是小编为大家收集整理的关于用spring为org.apache.commons.dbcp.BasicDataSource做日志连接池的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我试图使用log4j

记录org.apache.commons.dbcp.BasicDataSource的连接池

我正在使用弹簧框架进行DAO层注入.

当我在org.apache.commons.dbcp.BasicDataSource内看到代码时,没有使用记录器.因此,似乎不可能为我记录汇总消息.

但是我再次看到了这个链接 http:////forum.springs.orgce.org/springs.org/showthread.388306 - 连接式 - debug-info .
有人说要放 log4j.category.org.apache.dbcp=DEBUG.但是我找不到正确的答案.
所以我的问题是,可以使用log4j for org.apache.commons.dbcp.BasicDataSource?

使用log4j汇总日志

推荐答案

似乎BasicDataSource只有一个打印词,而不是作为成员变量的记录器.因此,您必须调用BasicDataSource.SetLogwriter(PrintWriter),其中PrintWriter只是包装Log4J Logger.

我遇到了这个: http://www.opensource.apple.com/source/jboss/jboss/jboss-737/jboss-all/common/common/src/main/org/org/jboss/logging/logging/loggerwriter.loggerwriter.java P>

似乎完全做到了.我不知道Apache Commons中的工具可以做类似的事情,但是上面的链接中的类似乎可以完成您想要的.

其他推荐答案

是为时已晚,因为问问题,但这是我解决问题的方式:

在JDBC URL

中将记录器指定为驱动程序
new BasicDataSource().setUrl("jdbc:mysql://localhost/DBName?logger=com.mysql.jdbc.log.Slf4JLogger&profileSQL=true");

本文地址:https://www.itbaoku.cn/post/1574878.html

问题描述

I am trying to log connection pooling for org.apache.commons.dbcp.BasicDataSource using log4j

I am using spring framework for dao layer injection.

When I saw code inside org.apache.commons.dbcp.BasicDataSource, Logger is not used .So it seems impossible to log pooling message for me.

But again I saw this link http://forum.springsource.org/showthread.php?38306-Connection-Pooling-debug-info.
Some people were saying to put log4j.category.org.apache.dbcp=DEBUG. But I could not find the right answer.
So my question is, can connection pooling log using log4j for org.apache.commons.dbcp.BasicDataSource?

推荐答案

It seems that BasicDataSource only has a PrintWriter, not a Logger as a member variable. So you'd have to call BasicDataSource.setLogWriter(printWriter) where the printWriter is simply wrapping your log4j logger.

I came across this: http://www.opensource.apple.com/source/JBoss/JBoss-737/jboss-all/common/src/main/org/jboss/logging/util/LoggerWriter.java

which seems to do exactly that. I don't know of a tool in Apache Commons that does something similar, but the class in the link above seems like it would accomplish what you are looking for.

其他推荐答案

Its too late since the question was asked but this is how I fixed the issue:

Specify the logger to the driver in JDBC URL

new BasicDataSource().setUrl("jdbc:mysql://localhost/DBName?logger=com.mysql.jdbc.log.Slf4JLogger&profileSQL=true");