Log4j正在运行但是hibernate日志没有创建到hibernate.log文件中

时间:2014-02-17 16:50:37

标签: java hibernate logging log4j

我的log4j正在运行,但是hibernate日志没有包含在我的hibernate.log文件中

我正在使用jobss 7.1.1 final

1)我在jobss 7.1.1 final中创建了模块 所以我的module.xml如下所示

<?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如下所示

<?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)我的log4j.xml(/WEB_INF/log4j.xml)如下

<?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)我在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)我在类路径中推出了以下jar文件。

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

请告诉我我必须做的hibernate日志记录的附加配置是什么。

所有日志文件都在创建,除了hibernate登录hibernate.log文件外,所有日志都在创建。

1 个答案:

答案 0 :(得分:1)

您是否有像hibernate.cfg.xml文件那样的其他hibernate配置?如果属性show sql设置为true,则可以将hibernate日志重定向到控制台。

如果不是,这是我使用的log4j配置的一个简单示例:

#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