如何为 Hibernate 的 JUnit 测试配置公共日志记录
我正在尝试使用 junit 测试和公共日志记录来调试 Spring 应用程序中的一些休眠功能,但除了默认的 INFO 消息之外,我似乎无法看到任何其他内容。我还从 Eclipse 运行这些 junit 测试。
我在春季论坛上也没有运气。
我对 Hibernate 的调试日志输出特别感兴趣(尝试找出为什么运行此测试需要 23 秒)。
当前输出显示 INFO 的默认设置:
Mar 29, 2011 4:44:35 PM org.springframework.test.AbstractTransactionalSpringContextTests onSetUp INFO: Began transaction: transaction manager [org.springframework.orm.hibernate3.HibernateTransactionManager@5f873eb2]; defaultRollback true testGetSubjectsForSite time: [00:00:00:068] Mar 29, 2011 4:44:58 PM org.springframework.test.AbstractTransactionalSpringContextTests endTransaction INFO: Rolled back transaction after test execution
我尝试将 commons-logging.properties 文件添加到类路径(与 hibernate.properties 和 test-components.xml 相同的位置),但仍然只显示默认的 INFO 消息。
这是 commons-logging.properties 文件:
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
# handlers
handlers=java.util.logging.ConsoleHandler
# default log level
.level=FINE
org.springframework.level=FINE
org.hibernate.level=FINE
# level for the console logger
java.util.logging.ConsoleHandler.level=FINE
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
有谁能够解释为什么我无法打印调试消息吗?我是否缺少日志记录设置?
编辑:我尝试过 FINEST 和 DEBUG 但无济于事。
I'm trying to debug some hibernate functionality in a spring app with a junit test and commons logging, but I can't seem to get anything other than the default INFO messages to appear. I'm also running these junit tests from Eclipse.
I've had no luck from the spring forums either.
I'm particularly interested in the debug logging output by Hibernate (to try and figure out why it takes 23 seconds to run this test).
Current output shows the default setting of INFO:
Mar 29, 2011 4:44:35 PM org.springframework.test.AbstractTransactionalSpringContextTests onSetUp INFO: Began transaction: transaction manager [org.springframework.orm.hibernate3.HibernateTransactionManager@5f873eb2]; defaultRollback true testGetSubjectsForSite time: [00:00:00:068] Mar 29, 2011 4:44:58 PM org.springframework.test.AbstractTransactionalSpringContextTests endTransaction INFO: Rolled back transaction after test execution
I've tried to add a commons-logging.properties file to the classpath (the same location as the hibernate.properties and test-components.xml) but still only the default INFO messages appear.
Here's the commons-logging.properties file:
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
# handlers
handlers=java.util.logging.ConsoleHandler
# default log level
.level=FINE
org.springframework.level=FINE
org.hibernate.level=FINE
# level for the console logger
java.util.logging.ConsoleHandler.level=FINE
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
Is anyone able to shed any light on why I can't get the DEBUG messages to print out? Is there a logging setting I'm missing?
Edit: I've tried FINEST and DEBUG to no avail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的默认和休眠日志记录处于“FINE”级别,这在 log4j 术语中更像是“INFO”。
您需要为 org.hibernate 设置 DEBUG 级别,该级别在 JDK 日志记录中等于“FINEST”
集
org.hibernate.level=FINEST(在上面的日志中应该启用调试日志)
Your default and hibernate logging is at Level "FINE" which is more of a "INFO" in log4j terms.
You need set DEBUG level for org.hibernate which in JDK logging is equal to 'FINEST'
Set
org.hibernate.level=FINEST (in the above log which should enable debug logs)
不幸的是,Jdk14Logger 使用的日志配置文件似乎应该在运行时指定。
请参阅 JDK 目录中的以下文件:JDK_HOME/jre/lib/logging.properties(如果未找到配置文件,则使用默认文件)
此外,文件路径应该是绝对路径,否则它是相对于执行 JRE 的文件夹 - 请参阅 java.util.logging.LogManager.readConfiguration() 的代码
另请参阅:
http://www.javapractices.com/topic/TopicAction.do?Id=143
http://cyntech.wordpress.com/2009/01/09/how-to-use-commons-logging/
Unfortunately, it seems the logging configuration file used by Jdk14Logger should be specified at runtime.
See the following file in your JDK directory: JDK_HOME/jre/lib/logging.properties (it's the default one used if no config file is found)
Moreover, the file path should be absolute, otherwise it's relative to the folder where the JRE is executed - see the code of java.util.logging.LogManager.readConfiguration()
Also see:
http://www.javapractices.com/topic/TopicAction.do?Id=143
http://cyntech.wordpress.com/2009/01/09/how-to-use-commons-logging/