Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j2 log hibernate parameters binding

Does anyone have an example of how to log hibernate sql parameters with log4j2? I know how to log them with log4j 1.x, but I couldn't do this with 2.0 beta 3 version (last version so far) of log4j.

Thanks.

like image 699
grozandrei Avatar asked Dec 28 '12 22:12

grozandrei


People also ask

Which log type shows which values are binding into the parameters?

SQL. The bind parameters are logged to the org. hibernate. type.


1 Answers

I've seen other responses to this question, but none that log just the sql substitution parameters. If you set the org.hibernate.type Logger category, you will get a TON of output!

All I want to see is the substituted values. If that's what you want as well, read on...

In summary the Logger category you want to configure is called:

org.hibernate.type.descriptor.sql.BasicBinder

And you need to set its value to TRACE

That said, below are .properties style and XML configurations that make this work.

.properties style:

log4j.rootLogger=INFO, Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n
# Below is the line to dump ONLY the bind variables
log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

XML:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <appender name="Console" class="org.apache.log4j.ConsoleAppender"> 
    <layout class="org.apache.log4j.PatternLayout"> 
      <param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/> 
    </layout> 
  </appender> 

  <root> 
    <priority value="DEBUG" /> 
    <appender-ref ref="Console" /> 
  </root>

  <logger name="org.hibernate.type.descriptor.sql.BasicBinder">
    <level value="TRACE" />
    <appender-ref ref="Console" />
  </logger>

</log4j:configuration>

The output will look like:

10:58:47,631 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (Thread-5212 (HornetQ-client-global-threads-475186579)) binding parameter [1] as [BIGINT] - 165
10:58:47,631 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (Thread-5212 (HornetQ-client-global-threads-475186579)) binding parameter [2] as [BIGINT] - 1390496100822

Have fun!

like image 74
J Steven Perry Avatar answered Oct 04 '22 21:10

J Steven Perry