3lm 3lm 3lm 3lm -4 years ago 88
Java Question

can't log Hibernate queries log4j

I am trying to log hibernate queries into a webapp specific log file.
I can log any message but I am still unable to log hibernate queries.

Them are being logged inside the

server.log
, even in a different file, but I need an app-specific log for each app running on the same server.

I am using
log4j 1.2
, I am able to create the log files, to log every information passed to console but hibernate queries.

Inside a
database.properties
file I found the
hibernate.show_sql=true
that is responsible for the logging output in console.
This is the kind of console's logging output I refer:


16:10:35,827 INFO [stdout] (http-localhost-127.0.0.1-8080-1) Hibernate: select [...](here is outputted the correct query passed, with the
?
of the prepared statement)


Anyway, what I need is to log that output.
I tried different
log4j.xml
settings, but still with no success, it creates log files but does not log those outputs.
Here is the current setting, that is logging everything inside the same file, when I will be able to log even those information I will go on separating the logs.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="mainFileAppender2" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="maxFileSize" value="100MB" />
<param name="maxBackupIndex" value="50" />
<param name="File" value="${webapp.root}/WEB-INF/logs/mainCORE.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c{1}:%L %m %n" />
</layout>
</appender>

<appender name="journaldev-hibernate2" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${webapp.root}/WEB-INF/logs/hib-queriesCORE.log" />
<param name="Append" value="false"/><!--value="true" /-->
<param name="ImmediateFlush" value="true" />
<param name="MaxFileSize" value="200MB" />
<param name="MaxBackupIndex" value="50" />

<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %d{Z} [%t] %-5p (%F:%L) - %m%n" />
</layout>
</appender>
<root>
<priority value="debug"></priority>
<!-- <appender-ref ref="stdout"/> -->
<appender-ref ref="mainFileAppender2"/>
</root>




This configuration logs everything but those outputs. In the
server.log
are logged as follows:


16:10:35,827 INFO [stdout] (http-localhost-127.0.0.1-8080-1) Hibernate: select [...](rest of the query)

Answer Source

Configure the logger for the following categories:

log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE

The first one will log the SQL-Statement (with ? for parameter values), the second one will print those parameter values if you need them.

So something like this should do the trick in your case:

<category name="org.hibernate.SQL">
    <appender-ref ref="journaldev-hibernate2"/>
   <priority value="DEBUG"/>
</category>

<category name="org.hibernate.type">
   <appender-ref ref="journaldev-hibernate2"/>
   <priority value="TRACE"/>
</category>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download