Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use logback to log sql parameters of spring JdbcTemplate

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.

like image 525
Maxim Kolesnikov Avatar asked Aug 24 '14 19:08

Maxim Kolesnikov


2 Answers

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

like image 175
Tadeu Jr. Avatar answered Oct 21 '22 02:10

Tadeu Jr.


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.

like image 31
Ricardo Veguilla Avatar answered Oct 21 '22 02:10

Ricardo Veguilla