java - BasicConfigurator 默认情况下不会在 JUnit 测试中记录堆栈跟踪

标签 java logging log4j apache-commons-logging

在运行一些JUnit 测试时,我从以下内容开始设置Log4J:

BasicConfigurator.configure()

然后我通过Commons Logging调用以下内容:

LogFactory.getLogger(this.getClass()).fatal(异常)

这不会打印堆栈跟踪(仅打印异常消息)。

我需要知道通过 JUnit 测试中使用的简化日志记录设置打印堆栈跟踪需要哪些步骤?我不希望仅仅为了 JUnit 测试而完全配置 Log4J,但如果必须,请告诉我。

最佳答案

这与记录器配置无关。

阅读 fatal(Object) 的 javadoc方法。它说:

"WARNING Note that passing a Throwable to this method will print the name of the Throwable but no stack trace. To print a stack trace use the fatal(Object, Throwable) form instead."

如果您希望日志包含堆栈跟踪,则必须使用该方法的 2 个参数版本。这也适用于其他日志级别的相应方法。

关于java - BasicConfigurator 默认情况下不会在 JUnit 测试中记录堆栈跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6234502/

相关文章:

java - SL4J 配置更改或正则表达式以屏蔽 Json 中的电子邮件(由 '@' 和 '.com' 识别)

java - 如何使用 Google Guava 的 Preconditions.checkElementIndex?

java - Android Manifest 文件定制

tomcat - log4j2 为 FileAppender 指定 tomcat 主目录的相对路径

java - “log4j:configuration” 必须匹配 “(renderer….loggerFactory)?)”

java - 使用雅虎财经报价时 jfreechart 崩溃

php - 如何在 Codeigniter 中更改日志文件扩展名

python - 如何为项目中的所有记录器设置处理程序?

spring - 如何在 log4j 中为 docx4j 关闭调试日志记录

Java:编写一个抛出任何 RuntimeException 的通用方法