How to log SQL queries, their parameters and results using Log4jdbc in Spring Boot projects?

To view the SQL queries sent to the database, we usually use the parameter showSql:

spring.jpa.showSql=true

This allows us to see the body of the operator, but not its parameters:

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

And especially we do not see the result of the request.

Is there a way to see the SQL statement, its parameters, and the result in the application log?

+2
source share
1 answer

JDBC Logging

log4jdbc-spring-boot-starter JDBC, Spring Boot/Spring Data JPA.

, JPQL :

select u from User u where u.name = 'john'

SQL- :

select ... from users users0_ where users0_.name='john'

:

|---|---------|
|id |name     |
|---|---------|
|1  |john     |
|---|---------|

, :

<dependency>
    <groupId>com.integralblue</groupId>
    <artifactId>log4jdbc-spring-boot-starter</artifactId>
    <version>1.0.2</version>
</dependency>

application.properties:

logging.level.jdbc.resultsettable=info
logging.level.jdbc.sqltiming=info
logging.level.jdbc.sqlonly=fatal
logging.level.jdbc.audit=fatal
logging.level.jdbc.resultset=fatal
logging.level.jdbc.connection=fatal

, log4jdbc, :

log4jdbc.dump.sql.addsemicolon=true
log4jdbc.dump.sql.maxlinelength=0
log4jdbc.trim.sql.extrablanklines=false
+3

Source: https://habr.com/ru/post/1683702/


All Articles