一尘不染

使用Log4j XML配置文件配置Hibernate日志记录?

hibernate

我还找不到关于如何使用Log4j的XML样式配置文件配置Hibernate日志记录的任何文档。

这是否有可能,或者我是否使用属性样式配置文件来控制Hibernate的日志记录?

如果有人有任何信息或文档链接,我们将不胜感激。

编辑:
只是为了澄清,我正在寻找控制Hibernate的实际XML语法的示例。

EDIT2:
这是我的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">
        <param name="Threshold" value="info"/>
        <param name="Target" value="System.out"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ABSOLUTE} [%t] %-5p %c{1} - %m%n"/>
        </layout>
    </appender>
    <appender name="rolling-file" class="org.apache.log4j.RollingFileAppender">
        <param name="file" value="Program-Name.log"/>
        <param name="MaxFileSize" value="1000KB"/>
    <!-- Keep one backup file -->
        <param name="MaxBackupIndex" value="4"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %l - %m%n"/>
        </layout>
    </appender>

    <root>
        <priority value ="debug" />
        <appender-ref ref="console" />
        <appender-ref ref="rolling-file" />
    </root>
</log4j:configuration>

日志记录工作正常,但是我正在寻找一种方法来降低和控制hibernate日志记录,使其与我的应用程序级别的日志记录分开,因为当前它正在淹没我的日志。我已经找到了使用首选项文件执行此操作的示例,我只是想知道如何在XML文件中执行此操作。


阅读 266

收藏
2020-06-20

共1个答案

一尘不染

来自http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-
configuration.html#configuration-
logging

以下是记录器类别的列表:

Category                    Function

org.hibernate.SQL           Log all SQL DML statements as they are executed
org.hibernate.type          Log all JDBC parameters
org.hibernate.tool.hbm2ddl  Log all SQL DDL statements as they are executed
org.hibernate.pretty        Log the state of all entities (max 20 entities) associated with the session at flush time
org.hibernate.cache         Log all second-level cache activity
org.hibernate.transaction   Log transaction related activity
org.hibernate.jdbc          Log all JDBC resource acquisition
org.hibernate.hql.ast.AST   Log HQL and SQL ASTs during query parsing
org.hibernate.secure        Log all JAAS authorization requests
org.hibernate               Log everything (a lot of information, but very useful for troubleshooting)

格式化以粘贴到log4j XML配置文件中:

<!-- Log all SQL DML statements as they are executed -->
<Logger name="org.hibernate.SQL" level="debug" />
<!-- Log all JDBC parameters -->
<Logger name="org.hibernate.type" level="debug" />
<!-- Log all SQL DDL statements as they are executed -->
<Logger name="org.hibernate.tool.hbm2ddl" level="debug" />
<!-- Log the state of all entities (max 20 entities) associated with the session at flush time -->
<Logger name="org.hibernate.pretty" level="debug" />
<!-- Log all second-level cache activity -->
<Logger name="org.hibernate.cache" level="debug" />
<!-- Log transaction related activity -->
<Logger name="org.hibernate.transaction" level="debug" />
<!-- Log all JDBC resource acquisition -->
<Logger name="org.hibernate.jdbc" level="debug" />
<!-- Log HQL and SQL ASTs during query parsing -->
<Logger name="org.hibernate.hql.ast.AST" level="debug" />
<!-- Log all JAAS authorization requests -->
<Logger name="org.hibernate.secure" level="debug" />
<!-- Log everything (a lot of information, but very useful for troubleshooting) -->
<Logger name="org.hibernate" level="debug" />

注意:大多数记录器使用DEBUG级别,但是org.hibernate.type使用TRACE。在以前的Hibernate版本中,org.hibernate.type也使用DEBUG,但是从Hibernate
3开始,您必须将级别设置为TRACE(或ALL),以便查看JDBC参数绑定日志记录。

并指定了这样的类别:

<logger name="org.hibernate">
    <level value="ALL" />
    <appender-ref ref="FILE"/>
</logger>

必须将其放在根元素之前。

2020-06-20