Logging SQL queries with Spring

If you need to see the SQL executed by Spring jdbc classes, you can use log4j configuration to turn on debug logging level on the org.springframework.jdbc.core package

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

This will leave in your log files very useful pieces of information:

  • the SQL
  • the values set for each query parameter
  • the number of rows affected

DEBUG core.JdbcTemplate - Executing SQL update [INSERT INTO analysisResult (ANALYZABLEID, ASIGNEEID, STARTDATE, ENDDATE, CODE, LIMIT, NUMINCIDENCES, NUMLIVEINCIDENCES, NUMLIVEINCIDENCESDAY, NUMLIVEINCIDENCESDAYANYONE) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]

DEBUG core.StatementCreatorUtils - Setting SQL statement parameter value: column index 1, parameter value [5], value class [java.lang.Long], SQL type -5

DEBUG core.JdbcTemplate - SQL update affected 1 rows

Hopefully, this will save you some minutes someday.

Posted in spring. 4 Comments »

4 Responses to “Logging SQL queries with Spring”

  1. 52lemurs Says:

    Thanks. This is exactly what I needed.

  2. Vyas Sanzgiri Says:

    What does category stand for ?

    Can you give a more generic template??

  3. Abel Muiño Says:

    “category” has been replaced by “logger” in recent log4j releases.

  4. Matt Says:

    Cool, thanks


Leave a Reply