Log4j is working but hibernate logs is not create

2019-06-12 04:43发布

问题:

My log4j is working but the hibernate log is not crating in to my hibernate.log file

I am using jobss 7.1.1 final so

1)I have crated module in jobss 7.1.1 final so my module.xml is as follw

<?xml version="1.0" encoding="UTF-8"?>  
<module xmlns="urn:jboss:module:1.1" name="com.fourthdti.myproject">  
    <resources>  
        <resource-root path="log4j-1.2.16.jar"/>  
    </resources>  
    <dependencies>
        <module name="javax.api"/>
    </dependencies>  
</module>

2) jboss-deployment-structure.xml is as follw

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <ear-subdeployments-isolated>false</ear-subdeployments-isolated>
    <deployment name="eSociety-ear.ear">
        <dependencies>
            <module name="com.fourthdti.myproject" export="true" />
        </dependencies>
        <exclusions>
            <module name="org.apache.commons.logging" />
            <module name="org.apache.log4j" />
            <module name="org.jboss.logging" />
            <module name="org.jboss.logging.jul-to-slf4j-stub" />
            <module name="org.jboss.logmanager" />
            <module name="org.jboss.logmanager.log4j" />
            <module name="org.slf4j" />
            <module name="org.slf4j.impl" />
            <module name="org.antlr"/>
            <module name="org.hibernate.*"/>
        </exclusions>
    </deployment>

    <sub-deployment name="eSociety-web-0.0.1-SNAPSHOT.war">
        <exclusions>
            <module name="org.apache.commons.logging" />
            <module name="org.apache.log4j" />
            <module name="org.jboss.logging" />
            <module name="org.jboss.logging.jul-to-slf4j-stub" />
            <module name="org.jboss.logmanager" />
            <module name="org.jboss.logmanager.log4j" />
            <module name="org.slf4j" />
            <module name="org.slf4j.impl" />
            <module name="org.antlr"/>
            <module name="org.hibernate.*"/>
        </exclusions>
    </sub-deployment>

    <sub-deployment name="eSociety-ejb-0.0.1-SNAPSHOT.jar">
        <exclusions>
            <module name="org.apache.commons.logging" />
            <module name="org.apache.log4j" />
            <module name="org.jboss.logging" />
            <module name="org.jboss.logging.jul-to-slf4j-stub" />
            <module name="org.jboss.logmanager" />
            <module name="org.jboss.logmanager.log4j" />
            <module name="org.slf4j" />
            <module name="org.slf4j.impl" />
            <module name="org.antlr"/>
            <module name="org.hibernate.*"/>
        </exclusions>
    </sub-deployment>
</jboss-deployment-structure>

3) my log4j.xml (/WEB_INF/log4j.xml) is as follow

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

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">

        <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
            <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n" />
            </layout>
        </appender>

        <appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
            <appender-ref ref="CONSOLE" />
            <appender-ref ref="FILE" />
        </appender>

        <appender name="allAppender" class="org.apache.log4j.RollingFileAppender"> 
          <param name="file" value="c:/logs/eSociety_All.log"/>
          <param name="MaxFileSize" value="1MB"/>
          <param name="MaxBackupIndex" value="2"/>
          <layout class="org.apache.log4j.PatternLayout"> 
            <param name="ConversionPattern" value="%d{ISO8601} [%d{z}] - %-5p [%t]: [%C] %m%n"/> 
          </layout> 
        </appender>

        <appender name="hibernateAppender" class="org.apache.log4j.RollingFileAppender"> 
          <param name="file" value="c:/logs/eSociety_Hibernate.log"/>
          <param name="MaxFileSize" value="1MB"/>
          <param name="MaxBackupIndex" value="2"/>
          <param name="Append" value="true"/>
          <layout class="org.apache.log4j.PatternLayout"> 
            <param name="ConversionPattern" value="%d{ISO8601} [%d{z}] - %-5p [%t]: [%C] %m%n"/> 
          </layout> 
          <filter class="org.apache.log4j.varia.StringMatchFilter">
            <param name="StringToMatch" value="bind" />
            <param name="AcceptOnMatch" value="true" />
          </filter>
          <filter class="org.apache.log4j.varia.StringMatchFilter">
            <param name="StringToMatch" value="select" />
            <param name="AcceptOnMatch" value="true" />
          </filter>  
          <filter class="org.apache.log4j.varia.DenyAllFilter"/>
        </appender>

        <appender name="springAppender" class="org.apache.log4j.RollingFileAppender"> 
          <param name="file" value="c:/logs/eSociety_Spring.log"/>
          <param name="MaxFileSize" value="1MB"/>
          <param name="MaxBackupIndex" value="2"/>
          <layout class="org.apache.log4j.PatternLayout"> 
            <param name="ConversionPattern" value="%d{ISO8601} [%d{z}] - %-5p [%t]: [%C] %m%n"/> 
          </layout> 
        </appender>

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

        <!-- Log the actual Hibernate Query Language (HQL) expression -->  
         <category name="org.hibernate.impl.SessionImpl">  
         <priority value="DEBUG">  
         </priority>  
         </category>  

        <!-- Log the substitution parameters in the HQL expression -->  
         <category name="org.hibernate.engine.QueryParameters">  
         <priority value="DEBUG">  
         </priority>  
         </category>

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

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

        <logger name="org.springframework" additivity="false">
                <level value="debug"/>
                <appender-ref ref="springAppender" />
        </logger>

        <root> 
          <priority value ="debug" /> 
          <appender-ref ref="allAppender" /> 
        </root>
</log4j:configuration>

4) I added the logging listener in web.xml

<!-- Logging listener -->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/log4j.xml</param-value>
    </context-param>

5) I have putted following jar files in class path.

i) log4j-1.2.16.jar
ii) slf4j-api-1.6.4.jar
iii) slf4j-log4j12-1.5.10.jar

Please suggest me what is the additional configuration for the hibernate logging I have to do.

All log file is creating, all logs is creating except hibernate log in hibernate.log file.

回答1:

Do you have additional hibernate configuration like an hibernate.cfg.xml file ? If the property show sql is set to true, hibernate logs could be re directed to console.

If it's not, here is a simple example of log4j configuration i use :

#Global variables
log.dir=/Users/XXX/Log
datestamp=yyyy-MM-dd/HH:mm:ss.SSS/zzz
date.pattern='.'yyyy-MM-dd

#Root level settings
log4j.rootLogger=INFO, file, stdout
log4j.logger.org.hibernate=INFO
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=ALL

#File logger appender (roll over at midnight each day)
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern=${date.pattern}
log4j.appender.file.File=${log.dir}/webapp.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{${datestamp}} [%t] %-5p %c{1}:%L - %m%n

#Console logger appender
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{${datestamp}} [%t] %-5p %c{1}:%L - %m%n