Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4jdbc PreparedStatement.toString()

Tags:

java

logging

jdbc

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?

like image 382
Vilius Avatar asked Apr 02 '26 17:04

Vilius


1 Answers

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.

like image 140
Vineet Reynolds Avatar answered Apr 04 '26 07:04

Vineet Reynolds



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!