bharal
bharal

Reputation: 16174

Hibernate - during an exception log sql and all parameters

How do I get hibernate to fully log all details when an exception is thrown? I would like to see the full (not truncated) sql and the full (not truncated) parameters.

ie Hibernate is throwing:

Caused by: org.hibernate.exception.DataException: could not execute statement
long stack trace
Caused by: java.sql.SQLDataException: Incorrect string value:
Query is: blah blah blah ... 

I don't want to see any "..." there. I want to see the full sql and the full parameter list.

This is an unchecked exception. What is the configuration needed for this (and any other hibernate exceptions) to be fully logged?

As this is production code I would prefer not changing the jdbc driver to net.sf.log4jdbc.DriverSpy

(Left out the truncated sql + parameters for clarity)

Upvotes: 3

Views: 580

Answers (2)

AhmedMaher
AhmedMaher

Reputation: 1771

Generally,(...) is used to indicate repeated logs lines in the stack trace, try increasing the value of
-XX:MaxJavaStackTraceDepth JVM parameter

Upvotes: 0

Elena Bondareva
Elena Bondareva

Reputation: 178

You can use p6spy library.

<dependency>
  <groupId>p6spy</groupId>
  <artifactId>p6spy</artifactId>
  <version>3.9.0</version>
</dependency>

The idea is wrapping the data source you use for Hibernate with p6spy proxy which logs everything as you configure it.

It may look like the following code if you use Hikari pool:

DataSource ds = new P6DataSource(new HikariDataSource(dsConfig));

The configuration of p6spy lays in spy.properties file which should be found in the root of your classpath (in the root of the jar file).

Here is the example of the spy.properties:

# suppress inspection "UnusedProperty" for whole file
#appender=com.p6spy.engine.spy.appender.StdoutLogger
appender=com.p6spy.engine.spy.appender.Slf4JLogger
excludecategories=info,debug,result,resultset
logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat
customLogMessageFormat=%(category) | connection %(connectionId) | \u001b[33m%(sqlSingleLine)\u001b[0m | %(executionTime) ms

As you can see, it is possible to configure how p6spy writes logs with the chosen appender.

If everything configured well, you see SQL queries and corresponding parameters in the logs.

Upvotes: 1

Related Questions