简体   繁体   English

如何为SpringJUnit4ClassRunner配置log4j.properties?

[英]How to configure log4j.properties for SpringJUnit4ClassRunner?

Suddenly this keeps happening during a JUnit test. 突然间,这在JUnit测试期间不断发生。 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.

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以使此日志记录正常工作。

Put a log4j.properties (or log4j.xml) file in the root of your test classpath . log4j.properties (或log4j.xml)文件放在测试类路径根目录中

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

An appender outputs to the console by default, but you can also explicitly set the target like this: 默认情况下,appender输出到控制台,但您也可以像这样显式设置目标:

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手册中找到

Log4J Logging will then be properly configured and this warning will disappear. 然后将正确配置Log4J Logging,此警告将消失。

If you don't want to bother with a file, you can do something like this in your code: 如果您不想打扰文件,可以在代码中执行以下操作:

static
{
    Logger rootLogger = Logger.getRootLogger();
    rootLogger.setLevel(Level.INFO);
    rootLogger.addAppender(new ConsoleAppender(
               new PatternLayout("%-6r [%p] %c - %m%n")));
}

Add a log4j.properties(log4j.xml) file with at least one appender in root of your classpath. 在类路径的根目录中添加至少一个appender的log4j.properties(log4j.xml)文件。

The contents of the file(log4j.properties) can be as simple as 文件的内容(log4j.properties)可以很简单

log4j.rootLogger=WARN,A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %c %x - %m%n

This will enable log4j logging with default log level as WARN and use the java console to log the messages. 这将启用log4j日志记录,默认日志级别为WARN并使用java控制台记录消息。

I have the log4j.properties configured properly. 我正确配置了log4j.properties。 That's not the problem. 那不是问题。 After a while I discovered that the problem was in Eclipse IDE which had an old build in "cache" and didn't create a new one (Maven dependecy problem). 过了一会儿,我发现问题出在Eclipse IDE中,它在“缓存”中有一个旧版本,并没有创建一个新版本(Maven依赖问题)。 I had to build the project manually and now it works. 我必须手动构建项目,现在它可以正常工作。

I was using Maven in eclipse and I did not want to have an additional copy of the properties file in the root folder. 我在eclipse中使用Maven,我不想在根文件夹中有一个属性文件的附加副本。 You can do the following in eclipse: 您可以在eclipse中执行以下操作:

  1. Open run dialog (click the little arrow next to the play button and go to run configurations) 打开运行对话框(单击播放按钮旁边的小箭头,然后转到运行配置)
  2. Go to the "classpath" tab 转到“类路径”选项卡
  3. Select the "User Entries" and click the "Advanced" button on the right side. 选择“用户条目”,然后单击右侧的“高级”按钮。
  4. Now select the "Add External folder" radio button. 现在选择“添加外部文件夹”单选按钮。
  5. Select the resources folder 选择资源文件夹

I know this is old, but I was having trouble too. 我知道这已经过时了,但我也遇到了麻烦。 For Spring 3 using Maven and Eclipse, I needed to put the log4j.xml in src/test/resources for the Unit test to log properly. 对于使用Maven和Eclipse的Spring 3,我需要将log4j.xml放在src / test / resources中以便Unit测试正确记录。 Placing in in the root of the test did not work for me. 放在测试的根部对我来说不起作用。 Hopefully this helps others. 希望这有助于其他人。

Because I don't like to have duplicate files (log4j.properties in test and main), and I have quite many test classes, they each runwith SpringJUnit4ClassRunner class, so I have to customize it. 因为我不喜欢有重复的文件(test和main中的log4j.properties),而且我有很多测试类,它们每个都运行SpringJUnit4ClassRunner类,所以我必须自定义它。 This is what I use: 这是我使用的:

import java.io.FileNotFoundException;

import org.junit.runners.model.InitializationError;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Log4jConfigurer;

public class MySpringJUnit4ClassRunner extends SpringJUnit4ClassRunner {

    static {
        String log4jLocation = "classpath:log4j-oops.properties";
        try {
            Log4jConfigurer.initLogging(log4jLocation);
        } catch (FileNotFoundException ex) {
            System.err.println("Cannot Initialize log4j at location: " + log4jLocation);
        }
    }

    public MySpringJUnit4ClassRunner(Class<?> clazz) throws InitializationError {
        super(clazz);
    }
}

When you use it, replace SpringJUnit4ClassRunner with MySpringJUnit4ClassRunner 使用它时,将SpringJUnit4ClassRunner替换为MySpringJUnit4ClassRunner

@RunWith(MySpringJUnit4ClassRunner.class) 
@ContextConfiguration("classpath:conf/applicationContext.xml") 
public class TestOrderController {
    private Logger LOG = LoggerFactory.getLogger(this.getClass());

    private MockMvc mockMvc;
...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM