何为SpringJUnit4ClassRunner配置log4j

何为SpringJUnit4ClassRunner配置log4j

本文介绍了如何为SpringJUnit4ClassRunner配置log4j.properties?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

突然间,这在JUnit测试期间不断发生。一切正常,我写了一些新的测试,发生了这个错误。如果我还原它,它就不会消失。为什么?

Suddenly this keeps happening during a JUnit test. Everything was working, I wrote some new tests and this error occured. If I revert it, it won't go away. Why is that?

log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.


推荐答案

您编写的新测试(直接或间接)使用使用Log4j 记录的类。

The new tests you wrote (directly or indirectly) use classes that log using Log4j.

需要配置Log4J才能使此日志记录正常工作。

Log4J needs to be configured for this logging to work properly.

log4j.properties (或log4j.xml)文件放在测试类路径的根目录

Put a log4j.properties (or log4j.xml) file in the root of your test classpath.

它应该有一些基本配置,例如

It should have some basic configuration such as

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# An alternative logging format:
# log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n

默认情况下,appender会输出到控制台,但您也可以显式设置目标如下:

An appender outputs to the console by default, but you can also explicitly set the target like this:

log4j.appender.A1.Target=System.out

这会将所有输出以漂亮的格式重定向到控制台。更多信息可以在中找到,

This will redirect all output in a nice format to the console. More info can be found here in the Log4J manual,

Log4J Logging将正确配置,此警告将消失。

Log4J Logging will then be properly configured and this warning will disappear.

这篇关于如何为SpringJUnit4ClassRunner配置log4j.properties?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 20:48