Search code examples
loggingspring-data-jdbc

How to log SQL statements and parameters with Spring Data JDBC


If you want to see what SQL statements Spring Data JDBC is executing, how would you do that? Since it uses bind variables, I also want to see their values in the logs.


Solution

  • Spring Data JDBC uses a JdbcTemplate to execute all SQL statements. So you really need to configure logging for that.

    The statements itself are logged by the template itself with DEBUG level. To see these you have to set the log level of org.springframework.jdbc.core.JdbcTemplate to DEBUG.

    The arguments for the bind variables are logged by org.springframework.jdbc.core.StatementCreatorUtils on TRACE level.

    How you configure those depends on your project setup. You can use the configuration options of Log4J, Logback or whatever logging implementation you are using. Or if you happen to use Spring Boot the following rows in the application.properties should do the trick.

    logging.level.org.springframework.jdbc.core.StatementCreatorUtils=TRACE
    logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG
    

    Or simply

    logging.level.org.springframework.jdbc.core = TRACE
    

    If you don't mind possibly getting some extra output.