hibernatelog4jtraceopen-session-in-view

logging hibernate queries with log4j when using servlet filter for open session in view pattern


First of all thank you for taking the time to read my question.

I'll start with my environment: Primefaces 3.5 Hibernate 4.3.2 Glassfish 3.1.2

My problem is this: I want to log the sql queries hibernate does using log4j(and the parameters bound to those queries). The logging used to work until I decided to implement the Open Session in View pattern. Now the logs only contain the information from this filter (like "Starting a database transaction", etc.).

My log4j.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
    <appender name="production"
              class="org.apache.log4j.rolling.RollingFileAppender">                        
        <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">

            <param name="ActiveFileName" value="app.log"/>  
            <param name="FileNamePattern" value="app.log.%d{yyyy-MM-dd}.gz" />

        </rollingPolicy>

        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                   value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1}:%L - %m%n" />
        </layout>
    </appender>

    <appender name="debugfile"
              class="org.apache.log4j.rolling.RollingFileAppender">
        <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">

            <param name="ActiveFileName" value="debug.log"/>  
            <param name="FileNamePattern" value="debug.log.%d{yyyy-MM-dd}.gz" />

        </rollingPolicy>        

        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                   value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1}:%L - %m%n" />
        </layout>        
    </appender>

    <appender name="hibernatefile"
              class="org.apache.log4j.rolling.RollingFileAppender">

        <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">

            <param name="ActiveFileName" value="hibernate.log"/>  
            <param name="FileNamePattern" value="hibernate.log.%d{yyyy-MM-dd}.gz" />

        </rollingPolicy>


        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                   value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1}:%L - %m%n" />
        </layout>
    </appender>


    <logger name="org.hibernate.type" additivity="false">
        <level value="TRACE" />
        <appender-ref ref="hibernatefile"/>
    </logger>

    <logger name="org.hibernate.SQL" >
        <level value="DEBUG" />
        <appender-ref ref="hibernatefile"/>
    </logger>


    <logger name="org.hibernate">
        <level value="info"/>
        <appender-ref ref="hibernatefile" />
    </logger>

    <root>        
        <appender-ref ref="debugfile" />
        <appender-ref ref="production" />        
    </root>
</log4j:configuration>

The trace, info and other information from hibernate are not printed anywhere.

Thank you for your help,

Andrei


Solution

  • Ok, so I think I reached a conclusion on this issue. There is not a problem with the combination of these technologies and/or patterns but with the version of Hibernate. I used Hibernate 3 before and the logging went just fine but according to this documentation http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch03.html#configuration-logging starting with Hibernate 4.0 they gave up on slf4j as the logging facade and they switched to JBoss logging. From this link (https://community.jboss.org/wiki/Logging) it seems that JBoss logging has a variant that uses log4j but unfortunately I haven't managed to configure the whole environment to use JBoss logging instead of slf4j. For my application I reverted to Hibernate 3 and the logging is going just fine.

    If there is someone that used Hibernate 4 with JBoss logging and log4j and has some sample configuration I think it would be appreciated by many people as I haven't found any relevant information in my searches.

    Thank you for any relevant information you might have.