how to log hibernate parameters

1.4k views Asked by At

I have project pf JPA. My logs looks like this:

Hibernate: insert into TEST(DESCRIPTION, NAME, version, id) values (?, ?, ?, ?)
[08/12/14 06:26:26:026 GET] TRACE sql.BasicBinder: binding parameter [1] as [VARCHAR] - [desc]
[08/12/14 06:26:26:026 GET] TRACE sql.BasicBinder: binding parameter [2] as [VARCHAR] - [name]
[08/12/14 06:26:26:026 GET] TRACE sql.BasicBinder: binding parameter [3] as [INTEGER] - [0]
[08/12/14 06:26:26:026 GET] TRACE sql.BasicBinder: binding parameter [4] as [BIGINT] - [21]

I have trace of org.hibernate.type in log4j configuration. That's nice, but is there any way to write this type of output?

Hibernate: insert into TEST(DESCRIPTION, NAME, version, id) values (desc, name, 0, 21)

I was searching that in google, I can't manage to find this type of solution. This output looks readable, and easy to read log files. Why this does not have hibenrate?

My configuration is like that:

<category name="org.hibernate.type">
    <priority value="trace" />
</category>
2

There are 2 answers

2
Andy Dufresne On BEST ANSWER

I doubt this could be possible out of the box from Hibernate. You could explicitly proxy the connection, statement and prepared statements that are created by your application (using a decorator design pattern) and log the parameters that passed.

0
Waqar On

Use slf4j + Log4j

<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.1</version> <!-- Use the latest version instead -->
    </dependency>

log4j.properties

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Log everything. Good for troubleshooting
log4j.logger.org.hibernate=INFO

# Log all JDBC parameters
log4j.logger.org.hibernate.type=ALL

hibernate.cfg.xml

<property name="show_sql">true</property>

Now parameters will be logged.