在运行一些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/