3

I'm trying to get the SQL statement after the variables are included using the log4jdbc framework. So far it catches and prints out the needed information, but also some stuff that I don't need:

11.06.2011 10:49:40 net.sf.log4jdbc.Slf4jSpyLogDelegator connectionOpened
INFO: 1. Connection opened
11.06.2011 10:49:40 net.sf.log4jdbc.Slf4jSpyLogDelegator methodReturned
INFO: 1. Connection.new Connection returned 
11.06.2011 10:49:40 net.sf.log4jdbc.Slf4jSpyLogDelegator methodReturned
INFO: 1. PreparedStatement.new PreparedStatement returned 
11.06.2011 10:49:40 net.sf.log4jdbc.Slf4jSpyLogDelegator methodReturned
INFO: 1. Connection.prepareStatement(INSERT INTO `xxx` (`y`, `y`, `y`, `y`, `y`, `y`) VALUES (NOW(), ?, ?, ?, ?, ?, ?) ) returned net.sf.log4jdbc.PreparedStatementSpy@13043d2
11.06.2011 10:49:40 net.sf.log4jdbc.Slf4jSpyLogDelegator methodReturned
INFO: 1. PreparedStatement.setInt(1, 0) returned 
11.06.2011 10:49:40 net.sf.log4jdbc.Slf4jSpyLogDelegator methodReturned
INFO: 1. PreparedStatement.setInt(2, 2) returned 
11.06.2011 10:49:40 net.sf.log4jdbc.Slf4jSpyLogDelegator methodReturned
INFO: 1. PreparedStatement.setLong(3, 1426656) returned 
11.06.2011 10:49:40 net.sf.log4jdbc.Slf4jSpyLogDelegator methodReturned
INFO: 1. PreparedStatement.setLong(4, 5177344) returned 
11.06.2011 10:49:40 net.sf.log4jdbc.Slf4jSpyLogDelegator methodReturned
INFO: 1. PreparedStatement.setInt(5, 12) returned 
11.06.2011 10:49:40 net.sf.log4jdbc.Slf4jSpyLogDelegator methodReturned
INFO: 1. PreparedStatement.setInt(6, 1) returned
2011-06-11 10:49:40,993;net.sf.log4jdbc.PreparedStatementSpy@13043d2;
11.06.2011 10:49:40 net.sf.log4jdbc.Slf4jSpyLogDelegator sqlOccured
INFO: INSERT INTO `xxx` (`y`, y`, `y`, `y`, `y`, `y`, `y`) VALUES (NOW(), 0, 2, 1426656, 5177344, 12, 1) 

How can I filter the entries and only show the actual, last, entry?

3

Having browsed the sources of log4jdbc, I could infer that all the logging is performed by slf4j, and delegated to the actual logger implementation in use by your application.

I'm unsure at to what logger is actually used in your environment. It would depend on your application server (I'm inferring this from your comment). Typically, application servers would use either log4j or java.util.logging.

You'll need to configure either of these loggers to ensure that only the jdbc.sqlonly logger is enabled; the other loggers mentioned on the log4jdbc page must be disabled, or have their logging levels set to a high value. If log4j is in use, the following snippet (from the example log4j properties file posted on the log4jdbc site), is the most relevant part of your logger configuration:

! Log only the SQL that is executed.
log4j.logger.jdbc.sqlonly=DEBUG,sql
log4j.additivity.jdbc.sqlonly=false

A similar configuration change must be performed if java.util.logging is being used.

| improve this answer | |
  • Ok, that sounds interesting, but I don't knwo where to insert this setting. I'm using logger = org.apache.log4j.Logger.getLogger("Logger"); and then logger.log(preparedStatement.toString()); So where can I change the log settings? – Vilius Jun 11 '11 at 9:57
  • Uh, why would you write code to create a new logger and log the statement? If you are relying on log4jdbc to wrap the connection object, I suppose (at least that was my impression on looking at the source code of the project) that it will take care of logging everything as well. On the topic of changing the setting, you'll need to know the location of the log4.properties used by the application server. Editing that file to include the new settings ought to be trivial; a restart might be required. – Vineet Reynolds Jun 11 '11 at 10:01
  • @Vilius, all the 5 loggers used by log4jdbc are managed by the SpyLogDelegator class in the library; you do not need to create any additional loggers to perform the logging. Enabling only the jdbc.sqlonly logger via the log4j.properties file, will ensure that only the SQL statements will be logged. – Vineet Reynolds Jun 11 '11 at 10:07
  • "why would you write code to create a new logger and log the statement?" - I use my own loggers since I want to log some of the statements to a file in my filesystem. But you are right, why should I create a new Logger, I am perhaps able to change the log4jdbc that it also logs the entries to a file. I will give it a try ... – Vilius Jun 11 '11 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.