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.
The new tests you wrote (directly or indirectly) use classes that log using Log4j.
Log4J needs to be configured for this logging to work properly.
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
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 will then be properly configured and this warning will disappear.
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.
The contents of the file(log4j.properties) can be as simple as
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.
I have the log4j.properties configured properly. 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). 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. You can do the following in eclipse:
- Open run dialog (click the little arrow next to the play button and go to run configurations)
- Go to the "classpath" tab
- Select the "User Entries" and click the "Advanced" button on the right side.
- Now select the "Add External folder" radio button.
- 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. 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. 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
@RunWith(MySpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:conf/applicationContext.xml")
public class TestOrderController {
private Logger LOG = LoggerFactory.getLogger(this.getClass());
private MockMvc mockMvc;
...
}