使用 Log4j XML 配置文件配置 Hibernate 日志记录? [英] Configuring Hibernate logging using Log4j XML config file?

查看:27
本文介绍了使用 Log4j XML 配置文件配置 Hibernate 日志记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I haven't been able to find any documentation on how to configure Hibernate's logging using the XML style configuration file for Log4j.

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

Is this even possible or do I have use a properties style configuration file to control Hibernate's logging?

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

If anyone has any information or links to documentation it would appreciated.


为了澄清起见,我正在寻找控制 Hibernate 的实际 XML 语法示例.


Just to clarify, I am looking for an example of the actual XML syntax to control Hibernate.


这是我的 XML 配置文件中的内容.


Here is what I have in my XML config file.

<?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>

日志记录工作正常,但我正在寻找一种方法来降低和控制休眠日志记录,这种方式与我的应用程序级别日志记录分开,因为它目前充斥着我的日志.我找到了使用首选项文件执行此操作的示例,我只是想知道如何在 XML 文件中执行此操作.

Logging works fine but I am looking for a way to step down and control the hibernate logging in way that separate from my application level logging, as it currently is flooding my logs. I have found examples of using the preference file to do this, I was just wondering how I can do this in a XML file.

推荐答案

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

这是记录器类别的列表:

Here's the list of logger categories:

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 配置文件中:

Formatted for pasting into a log4j XML configuration file:

<!-- 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 参数绑定日志.

NB: Most of the loggers use the DEBUG level, however org.hibernate.type uses TRACE. In previous versions of Hibernate org.hibernate.type also used DEBUG, but as of Hibernate 3 you must set the level to TRACE (or ALL) in order to see the JDBC parameter binding logging.

一个类别是这样指定的:

And a category is specified as such:

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

它必须放在根元素之前.

It must be placed before the root element.

这篇关于使用 Log4j XML 配置文件配置 Hibernate 日志记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆