使用 Log4j XML 配置文件配置 Hibernate 日志记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/436276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Configuring Hibernate logging using Log4j XML config file?
提问by James McMahon
I haven't been able to find any documentation on how to configure Hibernate's logging using the XML style configuration file for Log4j.
我找不到任何关于如何使用 Log4j 的 XML 样式配置文件配置 Hibernate 日志记录的文档。
Is this even possible or do I have use a properties style configuration file to control Hibernate's logging?
这甚至可能还是我使用属性样式配置文件来控制 Hibernate 的日志记录?
If anyone has any information or links to documentation it would appreciated.
如果有人有任何信息或文档链接,将不胜感激。
EDIT:
Just to clarify, I am looking for an example of the actual XML syntax to control Hibernate.
编辑:
只是为了澄清,我正在寻找控制 Hibernate 的实际 XML 语法的示例。
EDIT2:
Here is what I have in my XML config file.
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>
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.
日志记录工作正常,但我正在寻找一种方法来降低和控制休眠日志记录,这种方式与我的应用程序级别日志记录分开,因为它目前正在淹没我的日志。我找到了使用首选项文件执行此操作的示例,我只是想知道如何在 XML 文件中执行此操作。
回答by Loki
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)
Formatted for pasting into a log4j XML configuration file:
格式化以粘贴到 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" />
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.
注意:大多数记录器使用 DEBUG 级别,但是 org.hibernate.type 使用 TRACE。在先前版本的 Hibernate org.hibernate.type 中也使用了 DEBUG,但是从 Hibernate 3 开始,您必须将级别设置为 TRACE(或 ALL)才能查看 JDBC 参数绑定日志记录。
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.
它必须放在根元素之前。
回答by Dennis S
Loki's answerpoints to the Hibernate 3 docs and provides good information, but I was still not getting the results I expected.
Loki的回答指向 Hibernate 3 文档并提供了很好的信息,但我仍然没有得到我预期的结果。
Much thrashing, waving of arms and general dead mouse runs finally landed me my cheese.
剧烈的颠簸,挥舞着手臂和一般的死老鼠跑终于让我得到了我的奶酪。
Because Hibernate 3 is using Simple Logging Facade for Java(SLF4J) (per the docs), ifyou are relying on Log4j 1.2 you will alsoneed the slf4j-log4j12-1.5.10.jarif you are wanting to fullyconfigure Hibernate logging with a log4j configuration file. Hope this helps the next guy.
因为 Hibernate 3 使用Simple Logging Facade for Java(SLF4J)(根据文档),如果您依赖 Log4j 1.2,如果您想完全配置 Hibernate 日志记录,则还需要slf4j-log4j12-1.5.10.jarlog4j 配置文件。希望这对下一个人有所帮助。
回答by James McMahon
In response to homaxto's comment, this is what I have right now.
为了回应 homaxto 的评论,这就是我现在所拥有的。
<?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="debug"/>
<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="500KB"/>
<param name="MaxBackupIndex" value="4"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %l - %m%n"/>
</layout>
</appender>
<logger name="org.hibernate">
<level value="info" />
</logger>
<root>
<priority value ="debug" />
<appender-ref ref="console" />
<appender-ref ref="rolling-file" />
</root>
</log4j:configuration>
The key part being
关键部分是
<logger name="org.hibernate">
<level value="info" />
</logger>
Hope this helps.
希望这可以帮助。
回答by TMN
Here's what I use:
这是我使用的:
<logger name="org.hibernate">
<level value="warn"/>
</logger>
<logger name="org.hibernate.SQL">
<level value="warn"/>
</logger>
<logger name="org.hibernate.type">
<level value="warn"/>
</logger>
<root>
<priority value="info"/>
<appender-ref ref="C1"/>
</root>
Obviously, I don't like to see Hibernate messages ;) -- set the level to "debug" to get the output.
显然,我不喜欢看到 Hibernate 消息 ;) -- 将级别设置为“调试”以获取输出。
回答by dc360
The answers were useful. After the change, I got duplicate logging of SQL statements, one in the log4j log file and one on the standard console. I changed the persistence.xml file to say show_sql to false to get rid of logging from the standard console. Keeping format_sql true also affects the log4j log file, so I kept that true.
答案很有用。更改后,我得到了 SQL 语句的重复日志记录,一个在 log4j 日志文件中,一个在标准控制台上。我将 persistence.xml 文件更改为 show_sql 为 false 以摆脱标准控制台的日志记录。保持 format_sql 为真也会影响 log4j 日志文件,所以我保持为真。
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="myUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:d:\temp\database\cap1000;shutdown=true"></property>
<property name="dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
回答by Emilien Brigand
You can configure your log4jfile with the category tag like this (with a console appender for the example):
您可以log4j使用这样的类别标签配置您的文件(例如使用控制台附加程序):
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yy-MM-dd HH:mm:ss} %p %c - %m%n" />
</layout>
</appender>
<category name="org.hibernate">
<priority value="WARN" />
</category>
<root>
<priority value="INFO" />
<appender-ref ref="console" />
</root>
So every warning, error or fatal message from hibernate will be displayed, nothing more. Also, your code and library code will be in info level (so info, warn, error and fatal)
因此,将显示来自休眠的每个警告、错误或致命消息,仅此而已。此外,您的代码和库代码将处于信息级别(因此信息、警告、错误和致命)
To change log level of a library, just add a category, for example, to desactive spring info log:
要更改库的日志级别,只需添加一个类别,例如,取消激活 spring 信息日志:
<category name="org.springframework">
<priority value="WARN" />
</category>
Or with another appender, break the additivity (additivity default value is true)
或者用另一个appender,打破可加性(可加性默认值为true)
<category name="org.springframework" additivity="false">
<priority value="WARN" />
<appender-ref ref="anotherAppender" />
</category>
And if you don't want that hibernate log every query, set the hibernate property show_sqlto false.
如果您不希望每个查询都记录休眠,请将 hibernate 属性设置show_sql为false.

