15

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?

Reddy
  • 8,737
  • 11
  • 55
  • 73
abishkar bhattarai
  • 7,371
  • 8
  • 49
  • 66
  • I've seen similar comments to this effect myself and have never had luck getting any sort of logging from DBCP. Which is obviously quite annoying. It seems to be not possible unfortunately... :-/ – Michael May 21 '14 at 17:57
  • 1
    I have the same question, too!! I try to set following config in my log4j.xml, but no use. I am using dbcp 1.4 – petertc May 22 '14 at 09:40
  • 2
    http://stackoverflow.com/questions/3597219/logging-in-dbcp could be useful, as it seems it confirms that DBCP doesn't log. – reallynice Aug 01 '14 at 11:56

2 Answers2

5

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.

Tom Hartwell
  • 658
  • 1
  • 10
  • 21
4

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");
spartan
  • 88
  • 1
  • 7