How to log SQL queries and their results in Spring Data JPA projects with Log4jdbc-log4j2

To log SQL queries are sent to DB we are using this setting:


It allows us to see a statement body but not its parameters:

insert into master (id, version, name) values (null, ?, ?)

And also we don’t see a result of the query.

Is there a way to do this?

See my answer below how to use Log4jdbc-log4j2 to achieve this…

Log4jdbc-log4j2 is a very useful tool that can log SQL and/or JDBC calls and their results when you works with DB in Spring Data JPA projects.

It can allow us to log SQL statements and their parameters:

insert into master (id, version, name) values (null, 0, 'master1')

select as id1_1_, master0_.version as version2_1_, as name3_1_ from master master0_ where'master1'

And see their results in the table form:

|scope_identity() |
|1                |

|id |version  |name     |
|1  |0        |master1  |

How to use it:

  1. Include Log4jdbc-log4j2 dependency to the project:
  1. Replace you db connection (here example for H2):
  1. Add to the resources dir a file that contains one line:
  1. Add to the app properties file a Log4jdbc-log4j2 configuration, for example (you can play with these options):