Discussion:
How to use log4j (log4j.xml) and Maven and Junit??
Lisa
2007-01-23 22:16:30 UTC
Permalink
I have written some JUnit tests and put them in the standard layout for test
cases under Maven. The tests use log.debug(msg), log.info(msg) etc.

The tests run OK.

So now I created a simple log4j.xml file with an appender and a logger that
filters only the logging messages in my test case (by package).

When I run maven (mvn test), how do I tell it where my log4j.xml is and how to
use log4j logging?

I have tried everything. I edited the "mvn" script under the install directory
to include the following:

exec "$JAVACMD" \
$MAVEN_OPTS \
-classpath "${M2_HOME}"/core/boot/classworlds-*.jar \
"-Dclassworlds.conf=${M2_HOME}/bin/m2.conf" \
"-Dmaven.home=${M2_HOME}" \
"-Dmaven.junit.sysproperties=log4j.configuration" \
"-Dlog4j.configuration=file:C:\tmp\log4j.xml" \
${CLASSWORLDS_LAUNCHER} $QUOTED_ARGS


this does not work (have tried with and without file:.

---
I have included a something similiar on the mvn command line:
mvn -Dlog4j.configuration=c:\tmp\log4j.xml test

this does not work (have included =file:c:\tmp... as well

---
I have also tried setting properties in the pom.xml like so:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
<useFile>false</useFile>

<systemProperties>
<property>
<name>maven.junit.sysproperties</name>
<value>log4j.configuration</value>
</property>
<property>
<name>log4j.configuration</name>
<value>file=:c:\tmp\log4j.xml</value>
</property>
</systemProperties>
</configuration>
</plugin>

---
and

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
<useFile>false</useFile>

<systemProperties>
<property>
<name>maven.junit.sysproperties</name>
<value>log4j.configuration</value>
</property>
<property>
<name>log4j.configuration</name>
<value>c:\tmp\log4j.xml</value>
</property>
</systemProperties>
</configuration>
</plugin>

--- and (log4j.xml is in same directory as pom.xml) this does not work either.


<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
<useFile>false</useFile>

<systemProperties>
<property>
<name>maven.junit.sysproperties</name>
<value>log4j.configuration</value>
</property>
<property>
<name>log4j.configuration</name>
<value>log4j.xml</value>
</property>
</systemProperties>
</configuration>
</plugin>


---
none of these work. Any ideas on how to integrate Maven2, Log4J and Junit would
be greatly appreciated.
James Stauffer
2007-01-24 01:37:25 UTC
Permalink
Try putting the log4j.xml in the working directory and not providing a
path (just the filename). Also run with -Dlog4j.debug to see what
log4j finds.
Post by Lisa
I have written some JUnit tests and put them in the standard layout for test
cases under Maven. The tests use log.debug(msg), log.info(msg) etc.
The tests run OK.
So now I created a simple log4j.xml file with an appender and a logger that
filters only the logging messages in my test case (by package).
When I run maven (mvn test), how do I tell it where my log4j.xml is and how to
use log4j logging?
I have tried everything. I edited the "mvn" script under the install directory
exec "$JAVACMD" \
$MAVEN_OPTS \
-classpath "${M2_HOME}"/core/boot/classworlds-*.jar \
"-Dclassworlds.conf=${M2_HOME}/bin/m2.conf" \
"-Dmaven.home=${M2_HOME}" \
"-Dmaven.junit.sysproperties=log4j.configuration" \
"-Dlog4j.configuration=file:C:\tmp\log4j.xml" \
${CLASSWORLDS_LAUNCHER} $QUOTED_ARGS
this does not work (have tried with and without file:.
---
mvn -Dlog4j.configuration=c:\tmp\log4j.xml test
this does not work (have included =file:c:\tmp... as well
---
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
<useFile>false</useFile>
<systemProperties>
<property>
<name>maven.junit.sysproperties</name>
<value>log4j.configuration</value>
</property>
<property>
<name>log4j.configuration</name>
<value>file=:c:\tmp\log4j.xml</value>
</property>
</systemProperties>
</configuration>
</plugin>
---
and
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
<useFile>false</useFile>
<systemProperties>
<property>
<name>maven.junit.sysproperties</name>
<value>log4j.configuration</value>
</property>
<property>
<name>log4j.configuration</name>
<value>c:\tmp\log4j.xml</value>
</property>
</systemProperties>
</configuration>
</plugin>
--- and (log4j.xml is in same directory as pom.xml) this does not work either.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
<useFile>false</useFile>
<systemProperties>
<property>
<name>maven.junit.sysproperties</name>
<value>log4j.configuration</value>
</property>
<property>
<name>log4j.configuration</name>
<value>log4j.xml</value>
</property>
</systemProperties>
</configuration>
</plugin>
---
none of these work. Any ideas on how to integrate Maven2, Log4J and Junit would
be greatly appreciated.
---------------------------------------------------------------------
--
James Stauffer http://www.geocities.com/stauffer_james/
Are you good? Take the test at http://www.livingwaters.com/good/
Lisa
2007-01-24 06:03:14 UTC
Permalink
Post by James Stauffer
Try putting the log4j.xml in the working directory and not providing a
path (just the filename). Also run with -Dlog4j.debug to see what
log4j finds.
Thanks, where do I put -Dlog4j.debug?

I put it in the shell script that is run when I type "mvn" but nothing happened.
I did a "which mvn" and edited that file (mvn execution code listed in previous
post beginning with exec "$JAVACMD" \), but did not see anything different.

Also where is my working directory? Is it the directory I run mvn in? I put
log4j.xml in about every place I could think of with no luck.

Currently the log4j.xml is in the same directory as pom.xml where I run mvn test
from.

thanks, still trying to figure this one out with no luck.



Lisa
Jacob Kjome
2007-01-24 05:14:44 UTC
Permalink
Change:
-Dlog4j.configuration=c:\tmp\log4j.xml test

To:
-Dlog4j.configuration=file:///c:/tmp/log4j.xml test


Or, just copy log4j.xml to the root directory of your compiled test
classes and let Log4j autoconfigure itself.


Jake
Post by Lisa
I have written some JUnit tests and put them in the standard layout for test
cases under Maven. The tests use log.debug(msg), log.info(msg) etc.
The tests run OK.
So now I created a simple log4j.xml file with an appender and a logger that
filters only the logging messages in my test case (by package).
When I run maven (mvn test), how do I tell it where my log4j.xml is and how to
use log4j logging?
I have tried everything. I edited the "mvn" script under the
install directory
Post by Lisa
exec "$JAVACMD" \
$MAVEN_OPTS \
-classpath "${M2_HOME}"/core/boot/classworlds-*.jar \
"-Dclassworlds.conf=${M2_HOME}/bin/m2.conf" \
"-Dmaven.home=${M2_HOME}" \
"-Dmaven.junit.sysproperties=log4j.configuration" \
"-Dlog4j.configuration=file:C:\tmp\log4j.xml" \
${CLASSWORLDS_LAUNCHER} $QUOTED_ARGS
this does not work (have tried with and without file:.
---
mvn -Dlog4j.configuration=c:\tmp\log4j.xml test
this does not work (have included =file:c:\tmp... as well
---
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
<useFile>false</useFile>
<systemProperties>
<property>
<name>maven.junit.sysproperties</name>
<value>log4j.configuration</value>
</property>
<property>
<name>log4j.configuration</name>
<value>file=:c:\tmp\log4j.xml</value>
</property>
</systemProperties>
</configuration>
</plugin>
---
and
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
<useFile>false</useFile>
<systemProperties>
<property>
<name>maven.junit.sysproperties</name>
<value>log4j.configuration</value>
</property>
<property>
<name>log4j.configuration</name>
<value>c:\tmp\log4j.xml</value>
</property>
</systemProperties>
</configuration>
</plugin>
--- and (log4j.xml is in same directory as pom.xml) this does not work either.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
<useFile>false</useFile>
<systemProperties>
<property>
<name>maven.junit.sysproperties</name>
<value>log4j.configuration</value>
</property>
<property>
<name>log4j.configuration</name>
<value>log4j.xml</value>
</property>
</systemProperties>
</configuration>
</plugin>
---
none of these work. Any ideas on how to integrate Maven2, Log4J and Junit would
be greatly appreciated.
---------------------------------------------------------------------
Lisa
2007-01-24 06:17:27 UTC
Permalink
Post by Lisa
-Dlog4j.configuration=c:\tmp\log4j.xml test
-Dlog4j.configuration=file:///c:/tmp/log4j.xml test
Or, just copy log4j.xml to the root directory of your compiled test
classes and let Log4j autoconfigure itself.
Jake
Still no luck with either option mentioned. I searched on where maven stuck the
.class files and copied my log4j.xml there in the same directory. No luck.

I did a $which mvn and found mvn, which is a text file and edited it where it
runs maven, specifically:

exec "$JAVACMD" \
$MAVEN_OPTS \
-classpath "${M2_HOME}"/core/boot/classworlds-*.jar \
"-Dclassworlds.conf=${M2_HOME}/bin/m2.conf" \
"-Dmaven.home=${M2_HOME}" \
"-Dlog4j.configuration=file:///C:/tmp/log4j.xml" \
"-Dlog4j.debug" \
${CLASSWORLDS_LAUNCHER} $QUOTED_ARGS

no luck with the above. Trying to run maven, which is using the surefire plugin
to run tests (.java) which contain log.debug(msg) and have tried everything.

I am sure this is the script that is running mvn. I commented out the exec
"JAVACMD" section completely and nothing ran so I know it is hitting this code
to run mvn.

When I run the test cases it always says:
[INFO] [surefire:test]
[INFO] Surefire report directory:
C:\home\projects\branches\rel\sql\target\surefire-reports
log4j:WARN No appenders could be found for logger (com.presence.sql.DbTestCase).
log4j:WARN Please initialize the log4j system properly.

---
my log4j.xml has an appender like so (copied from another area where log4j is
working with a file just like this on another system):

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<!-- -->
<log4j:configuration
xmlns:log4j="http://jakarta.apache.org/log4j/"
disable="debug"
debug="true">

<!-- ******************************************************************* -->
<!-- all org.apache messages -->
<appender name="DBUNIT.DEBUG.LOG"
class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="logs/dbunit.debug.log"/>
<param name="Append" value="false"/>
<param name="ImmediateFlush" value="true"/>
<param name="Threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d %-5p [%t] %c %C (%F:%L) - %m\n"/>
</layout>
</appender>


<!-- *** LOGGERS *************************** -->
<!-- DBUNIT (DEBUG) -->
<logger
name="com.company.sql"
additivity="false">
<level value="DEBUG"/>
<appender-ref ref="DBUNIT.DEBUG.LOG"/>
</logger>
</log4j:configuration>
Jacob Kjome
2007-01-24 18:29:53 UTC
Permalink
Try adding something like the following to your log4j.xml, after the existing
<logger> entry...

<root>
<level value="warn"/>
<appender-ref ref="DBUNIT.DEBUG.LOG"/>
</root>

You can always create a separate appender for loggers other than
"com.company.sql" instead of using the "DBUNIT.DEBUG.LOG" appender for the
<root> logger.

I think the log4j.xml file is being found. You just haven't specified any
appenders for any loggers other than those named after your own package
namespace.


Jake
Post by Lisa
Post by Lisa
-Dlog4j.configuration=c:\tmp\log4j.xml test
-Dlog4j.configuration=file:///c:/tmp/log4j.xml test
Or, just copy log4j.xml to the root directory of your compiled test
classes and let Log4j autoconfigure itself.
Jake
Still no luck with either option mentioned. I searched on where maven stuck the
.class files and copied my log4j.xml there in the same directory. No luck.
I did a $which mvn and found mvn, which is a text file and edited it where it
exec "$JAVACMD" \
$MAVEN_OPTS \
-classpath "${M2_HOME}"/core/boot/classworlds-*.jar \
"-Dclassworlds.conf=${M2_HOME}/bin/m2.conf" \
"-Dmaven.home=${M2_HOME}" \
"-Dlog4j.configuration=file:///C:/tmp/log4j.xml" \
"-Dlog4j.debug" \
${CLASSWORLDS_LAUNCHER} $QUOTED_ARGS
no luck with the above. Trying to run maven, which is using the surefire plugin
to run tests (.java) which contain log.debug(msg) and have tried everything.
I am sure this is the script that is running mvn. I commented out the exec
"JAVACMD" section completely and nothing ran so I know it is hitting this code
to run mvn.
[INFO] [surefire:test]
C:\home\projects\branches\rel\sql\target\surefire-reports
log4j:WARN No appenders could be found for logger
(com.presence.sql.DbTestCase).
log4j:WARN Please initialize the log4j system properly.
---
my log4j.xml has an appender like so (copied from another area where log4j is
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<!-- -->
<log4j:configuration
xmlns:log4j="http://jakarta.apache.org/log4j/"
disable="debug"
debug="true">
<!-- ******************************************************************* -->
<!-- all org.apache messages -->
<appender name="DBUNIT.DEBUG.LOG"
class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="logs/dbunit.debug.log"/>
<param name="Append" value="false"/>
<param name="ImmediateFlush" value="true"/>
<param name="Threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d %-5p [%t] %c %C (%F:%L) - %m\n"/>
</layout>
</appender>
<!-- *** LOGGERS *************************** -->
<!-- DBUNIT (DEBUG) -->
<logger
name="com.company.sql"
additivity="false">
<level value="DEBUG"/>
<appender-ref ref="DBUNIT.DEBUG.LOG"/>
</logger>
</log4j:configuration>
---------------------------------------------------------------------
Gallagher, Ron
2007-01-24 14:44:34 UTC
Permalink
Lisa --

You don't need to make any modifications to the mvn script to accomplish
what you're trying to do. Here's what we do...

1) Set up a directory that will contain resource/configuration files
that you want available when you run your tests. For us, we use
${basedir}/src/test/config

2) Put any resource/configiguration files you want available during
tests in that directory. For your situation, you'd put the log4j.xml
file in the ${basedir}/src/test/config directory.

3) Add an entry to the build/testResources element in your pom.xml.
Here's an example:
<build>
<testResources>
<testResource>
<directory>src/test/config</directory>
<includes>
<include>log4j.xml</include>
</includes>
</testResource>
</testResources>
</build>
You need to add an 'include' entry for each resource. Check out
http://maven.apache.org/ref/current/maven-model/maven.html#class_testRes
ource for more details on what you can do with the testResources
element.

With these changes in place, Maven will copy to the test/classes folder
all of the test resources you've listed. The test/classes folder is
included in the class path when Maven runs your tests, so log4j will
utilize the log4j.xml file that was originally located in
src/test/config.

As far as turning on log4j's debugging, just set the debug attribute in
the root element to "true".

Ron Gallagher
Cingular Wireless

-----Original Message-----
From: news [mailto:***@sea.gmane.org] On Behalf Of Lisa
Sent: Tuesday, January 23, 2007 5:17 PM
To: log4j-***@logging.apache.org
Subject: How to use log4j (log4j.xml) and Maven and Junit??

I have written some JUnit tests and put them in the standard layout for
test
cases under Maven. The tests use log.debug(msg), log.info(msg) etc.

The tests run OK.

So now I created a simple log4j.xml file with an appender and a logger
that
filters only the logging messages in my test case (by package).

When I run maven (mvn test), how do I tell it where my log4j.xml is and
how to
use log4j logging?

I have tried everything. I edited the "mvn" script under the install
directory
to include the following:

exec "$JAVACMD" \
$MAVEN_OPTS \
-classpath "${M2_HOME}"/core/boot/classworlds-*.jar \
"-Dclassworlds.conf=${M2_HOME}/bin/m2.conf" \
"-Dmaven.home=${M2_HOME}" \
"-Dmaven.junit.sysproperties=log4j.configuration" \
"-Dlog4j.configuration=file:C:\tmp\log4j.xml" \
${CLASSWORLDS_LAUNCHER} $QUOTED_ARGS


this does not work (have tried with and without file:.

---
I have included a something similiar on the mvn command line:
mvn -Dlog4j.configuration=c:\tmp\log4j.xml test

this does not work (have included =file:c:\tmp... as well

---
I have also tried setting properties in the pom.xml like so:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
<useFile>false</useFile>

<systemProperties>
<property>
<name>maven.junit.sysproperties</name>
<value>log4j.configuration</value>
</property>
<property>
<name>log4j.configuration</name>
<value>file=:c:\tmp\log4j.xml</value>
</property>
</systemProperties>
</configuration>
</plugin>

---
and

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
<useFile>false</useFile>

<systemProperties>
<property>
<name>maven.junit.sysproperties</name>
<value>log4j.configuration</value>
</property>
<property>
<name>log4j.configuration</name>
<value>c:\tmp\log4j.xml</value>
</property>
</systemProperties>
</configuration>
</plugin>

--- and (log4j.xml is in same directory as pom.xml) this does not work
either.


<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
<useFile>false</useFile>

<systemProperties>
<property>
<name>maven.junit.sysproperties</name>
<value>log4j.configuration</value>
</property>
<property>
<name>log4j.configuration</name>
<value>log4j.xml</value>
</property>
</systemProperties>
</configuration>
</plugin>


---
none of these work. Any ideas on how to integrate Maven2, Log4J and
Junit would
be greatly appreciated.


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-***@logging.apache.org
For additional commands, e-mail: log4j-user-***@logging.apache.org
gerold kathan
2007-02-24 13:29:42 UTC
Permalink
hi - i have exactly the same issue:
* i did what you mentioned (1-3)
* yes it copies the log4j.xml to the right place under src/test/
* BUT the testrunner the complains that it cannot find the file under the
projects root directory (where also the pom.xml sits)

log4j:ERROR Could not parse file [log4j.xml].
java.io.FileNotFoundException: D:\_devel\_java\_workspace\springtest\log4j.xml

=> but he should look under

D:\_devel\_java\_workspace\springtest\target\test-classes\log4j.xml


=> the call from the java source to the log4j.xml looks like
static
{
DOMConfigurator.configure("log4j.xml");
logger = Logger.getLogger(AppTest.class);
}


strange - any hints on that ?
Jacob Kjome
2007-02-24 17:26:54 UTC
Permalink
Post by gerold kathan
* i did what you mentioned (1-3)
* yes it copies the log4j.xml to the right place under src/test/
* BUT the testrunner the complains that it cannot find the file under the
projects root directory (where also the pom.xml sits)
log4j:ERROR Could not parse file [log4j.xml].
D:\_devel\_java\_workspace\springtest\log4j.xml
Post by gerold kathan
=> but he should look under
D:\_devel\_java\_workspace\springtest\target\test-classes\log4j.xml
=> the call from the java source to the log4j.xml looks like
static
{
DOMConfigurator.configure("log4j.xml");
logger = Logger.getLogger(AppTest.class);
}
strange - any hints on that ?
Not strange at all. You are giving a relative file path which will
be resolved to the directory where the JVM started, which is likely
the directory of the pom.xml. Why are you manually configuring Log4j
anyway? Just copy log4j.xml to "test-classes" and let Log4j's
autoconfiguration perform the configuration. There is no reason to
manually call configure().


Jake

Loading...