How to configure log4j.properties for SpringJUnit4ClassRunner? How to configure log4j.properties for SpringJUnit4ClassRunner? spring spring

How to configure log4j.properties for SpringJUnit4ClassRunner?


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.PatternLayoutlog4j.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.ConsoleAppenderlog4j.appender.A1.layout=org.apache.log4j.PatternLayoutlog4j.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.