Use logback to log SQL parameters in Spring JdbcTemplate
Asked Answered
M

4

9

I'm using Logback with Spring JdbcTemplate to log my SQL queries. My configurations contains next line:

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

But this logs only query with wildcards ? without list of parameters.

Here on SO I found a few answers how to achieve parameters logging with log4j. But I don't want to switch to log4j.

So how can I receive parameters list for JdbcTemplate with Logback?

Edit

Actually, I'm using NamedParameterJdbcTemplate, if it matters.

Monique answered 24/8, 2014 at 19:28 Comment(0)
R
13

Try this

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

this will show log:

Setting SQL statement parameter value: column index 1, parameter value [1234], value class [java.lang.Integer], SQL type unknown

Rooster answered 11/11, 2016 at 19:6 Comment(1)
It's a bit verbose, but it gets the job done in an out-of-the-box sort of way. Thanks!Hemia
E
1

I usually prefer handling SQL statement logging at the DataSource or JDBC driver level.

I use BoneCP DataSource/Connection Pool library, which include support for statement logging via SLF4J and many other usefull features.

If changing your DataSource/Connection Pool library is not an option, maybe you can use log4jdbc which works as a proxy jdbc driver, which logs statements to SLF4J before calling the actual jdbc driver that talks to the database.

Excommunicate answered 24/8, 2014 at 20:54 Comment(0)
D
0

We can use as follow :

org.hibernate.type.descriptor.sql.

See Link: https://thorben-janssen.com/hibernate-logging-guide/

Dovecote answered 9/3, 2022 at 7:11 Comment(2)
<logger name="org.hibernate.type.descriptor.sql" level="TRACE" additivity="false" > <appender-ref ref="jdbc-transaction-log"/> </logger>Dovecote
Please edit according to How to Answer, using stackoverflow.com/editing-help and add the info from comments to the answer itself.Tijerina
C
0

If the spring boot version is lower than 5.1 you will not get results. The parameters and the query are treated as separate objects, so the only result you will find in your log will be something like "....AND (A.COD)=TRIM(?)" Extending NamedParameterJdbcTemplate I think is the only solution.

Caramelize answered 18/1 at 12:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.