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.
编辑:为了澄清,我正在寻找一个实际的XML语法示例来控制Hibernate。
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.
日志记录工作很好,但是我正在寻找一种方法,可以从我的应用程序级别的日志记录中分离和控制hibernate日志,因为它目前正在淹没我的日志。我找到了使用首选项文件进行此操作的示例,我想知道如何在XML文件中进行此操作。
6 个解决方案
#1
143
从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)
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.
NB:大多数日志记录器使用调试级别,但是org.hibernate。类型使用痕迹。在Hibernate的前几个版本中。类型也使用了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.
它必须放在根元素之前。
#2
25
Loki's answer points 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), if you are relying on Log4j 1.2 you will also need the slf4j-log4j12-1.5.10.jar if you are wanting to fully configure Hibernate logging with a log4j configuration file. Hope this helps the next guy.
因为Hibernate 3使用的是Java (SLF4J)的简单日志Facade(每个文档),如果您依赖Log4j 1.2,那么您还需要SLF4J -log4j12-1.5.10。如果您想要使用log4j配置文件完全配置Hibernate日志,那么jar。希望这能帮助下一个人。
#3
7
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.
希望这个有帮助。
#4
5
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消息;)——将级别设置为“debug”以获得输出。
#5
3
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日志文件中,一个在标准控制台。我改变了持久性。将show_sql写入到false的xml文件,以从标准控制台删除日志记录。让format_sql为true也会影响log4j日志文件,因此我将其保留为true。
<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>
#6
0
You can configure your log4j
file 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)
所以所有来自hibernate的警告、错误或致命消息都会显示出来,仅此而已。此外,您的代码和库代码将处于info级别(因此info、warn、error和致命性)
To change log level of a library, just add a category, for example, to desactive spring info log:
要更改一个库的日志级别,只需添加一个类别,例如,到desactive spring info日志:
<category name="org.springframework">
<priority value="WARN" />
</category>
Or with another appender, break the additivity (additivity default value is true)
或者使用另一个附加程序,打破可加性(可加性默认值为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_sql
to false
.
如果不希望hibernate记录每个查询,则将hibernate属性show_sql设置为false。
#1
143
从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)
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.
NB:大多数日志记录器使用调试级别,但是org.hibernate。类型使用痕迹。在Hibernate的前几个版本中。类型也使用了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.
它必须放在根元素之前。
#2
25
Loki's answer points 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), if you are relying on Log4j 1.2 you will also need the slf4j-log4j12-1.5.10.jar if you are wanting to fully configure Hibernate logging with a log4j configuration file. Hope this helps the next guy.
因为Hibernate 3使用的是Java (SLF4J)的简单日志Facade(每个文档),如果您依赖Log4j 1.2,那么您还需要SLF4J -log4j12-1.5.10。如果您想要使用log4j配置文件完全配置Hibernate日志,那么jar。希望这能帮助下一个人。
#3
7
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.
希望这个有帮助。
#4
5
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消息;)——将级别设置为“debug”以获得输出。
#5
3
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日志文件中,一个在标准控制台。我改变了持久性。将show_sql写入到false的xml文件,以从标准控制台删除日志记录。让format_sql为true也会影响log4j日志文件,因此我将其保留为true。
<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>
#6
0
You can configure your log4j
file 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)
所以所有来自hibernate的警告、错误或致命消息都会显示出来,仅此而已。此外,您的代码和库代码将处于info级别(因此info、warn、error和致命性)
To change log level of a library, just add a category, for example, to desactive spring info log:
要更改一个库的日志级别,只需添加一个类别,例如,到desactive spring info日志:
<category name="org.springframework">
<priority value="WARN" />
</category>
Or with another appender, break the additivity (additivity default value is true)
或者使用另一个附加程序,打破可加性(可加性默认值为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_sql
to false
.
如果不希望hibernate记录每个查询,则将hibernate属性show_sql设置为false。