4

I'm researching spring for a possible switch to a spring stack. One of the things that I thought was cool was the ability for spring jdbc to log all the executed sql. So I put in log4j, set up a log4j.properties file. and no sql.

here is the log4j.properties file:

log4j.appender.stdout=org.apache.log4j.ConsoleAppe nder
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.Patt ernLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=debug, stdout
log4j.category.org.springframework.jdbc.core=DEBUG

here is the output for some really simple insert sql via spring jdbc: http://pastie.org/713189

C. Ross
  • 31,137
  • 42
  • 147
  • 238
phil swenson
  • 8,564
  • 20
  • 74
  • 99

5 Answers5

11

Try setting these additional log4j loggers. The first will spit out the SQL that passes through spring's JdbcTemplate, the second gives you parameter values that Spring sets on prepared statements.

<logger name="org.springframework.jdbc.core.JdbcTemplate">
  <level value="debug" />
</logger>

<logger name="org.springframework.jdbc.core.StatementCreatorUtils">
  <level value="debug" />
</logger>

Clearly this is only going to work if you're directly or indirectly executing SQL using JdbcTemplate.

serg10
  • 31,923
  • 16
  • 73
  • 94
2

Try

log4j.category.org.springframework.jdbc.core = TRACE

This helped me, DEBUG just wasn't enough. I am using org.springframework.jdbc-3.0.6.RELEASE.jar with log4j-1.2.15 and slf4j (1.6.4)

For just an SQL (i.e. if you're not interested in bound parameter values) DEBUG should be enough.

Kroky
  • 519
  • 4
  • 6
2

Are you sure this is the log4.properties that your application is picking up? I copied the log4j.properties you posted into a Spring application on my local machine, and I got a ton of Spring debug entries in addition to the JDBC logging. I don't see debug entries like that in your output.

A few probable culprits for your log4j.properties not getting read correctly are:

  • log4j.properties isn't on your classpath. You can trying doing a class.getResource() on it to see if it's finding it at all.
  • There's another log4j.properties on your classpath.
  • Something is making commons-logging choose to use a different logger. You can find instructions for turning on the commons-logging diagnositics here
Jason Gritman
  • 5,251
  • 4
  • 30
  • 38
0

From my understanding spring will print out information when you use prepared statement.

See this discussion on Spring's forum.

DJ.
  • 6,664
  • 1
  • 33
  • 48
0

How are you executing the SQL? Spring won't magically log SQL that gets sent to the database, you have to go through the appropriate channels.

For example, if you execute SQL by using JdbcTemplate, either directly or via JdbcDaoSupport, then yes, the SQL will be logged for some operations, but only those operations that involve direct SQL.

If you use Hibernate or prepared statements, then Spring never gets to see the SQL itself, and therefore cannot log it.

If you posted some sample code that demonstrated how you were executing your SQL, it would help a lot.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • I'm using Spring's JdbcTemplate. I'm actually using some copyrighted sample code (from Enterprise Spring Recicpes), so don't think I should post it. I will try to hack out my own example though, that is my next step. – phil swenson Nov 24 '09 at 20:39