7

When running tests using maven, i'd like to see output on my screen.

After putting the log4j.xml into my project's dir (src/test/resources/cfg/), i updated my pom to include

235             <plugin>
236                 <groupId>org.apache.maven.plugins</groupId>
237                 <artifactId>maven-surefire-plugin</artifactId>
238                 <configuration>
239                     <skipTests>false</skipTests>
240                     <excludes>
241                         <exclude>**/*$*.java</exclude>
242                     </excludes>
243                         <systemProperties>
244                            <property>
245                               <name>-Dlog4j.configuration</name>
246                               <value>file:src/test/resources/cfg/log4j.xml</value>
247                            </property>
248                         </systemProperties>
249                     <additionalClasspathElements>
250                         <additionalClasspathElement>src/test/resources/cfg/</additionalClasspathElement>
251                     </additionalClasspathElements>
252                 </configuration>
253             </plugin>

The above does not work. When tests run, i still see the following message

log4j:WARN Please initialize the log4j system properly.

For the record, the following is the log4j

  1 <?xml version="1.0" encoding="UTF-8" ?>
  2 <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
  3 
  4 <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  5   <appender name="console" class="org.apache.log4j.ConsoleAppender">
  6     <param name="Target" value="System.out"/>
  7     <layout class="org.apache.log4j.PatternLayout">
  8       <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
  9     </layout>
 10   </appender>
 11 
 12   <root>
 13     <priority value ="debug" />
 14     <appender-ref ref="console" />
 15   </root>
 16 
 17 </log4j:configuration>

What am i missing please?

Yasin Okumuş
  • 2,299
  • 7
  • 31
  • 62
James Raitsev
  • 92,517
  • 154
  • 335
  • 470
  • possible duplicate of [How to show log4j output in surefire test resports](http://stackoverflow.com/questions/4379996/how-to-show-log4j-output-in-surefire-test-resports) – eis Nov 15 '12 at 21:15
  • You need a real file name and not the `file:src/test/resources/cfg/log4j.xml` URL. – Thorbjørn Ravn Andersen Nov 15 '12 at 21:20

2 Answers2

8

I have a couple of comments. First, I recommend that you put any test-related log4j, logback config files directly in src/test/resources. That way they are always added to the classpath and it just works.

However, if you need to put the files in src/test/resources/cfg and want to use the classpathelements, then heed the advice of the documentation

But, if you must, you can use the additionalClasspathElements element to add custom resources/jars to your classpath. This will be treated as an absolute file system path, so you may want use ${basedir} or another property combined with a relative path.

So try:

<additionalClasspathElement>${basedir}/src/test/resources/cfg/</additionalClasspathElement>
Guido Simone
  • 7,912
  • 2
  • 19
  • 21
-3

log4j:WARN Please initialize the log4j system properly.

You don't need to care this message if you don't need to use log4j.

However, if you need log4j, please add the following <dependency> tag under <dependencies>

<dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>1.2.16</version>
</dependency>
bhuang3
  • 3,493
  • 2
  • 17
  • 17