You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Lisa <ap...@purpleblade.net> on 2007/01/23 23:16:30 UTC

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-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: How to use log4j (log4j.xml) and Maven and Junit??

Posted by Lisa <ap...@purpleblade.net>.
James Stauffer <stauffer.james <at> gmail.com> writes:

> 
> 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


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: How to use log4j (log4j.xml) and Maven and Junit??

Posted by James Stauffer <st...@gmail.com>.
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.

On 1/23/07, Lisa <ap...@purpleblade.net> wrote:
> 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-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>


-- 
James Stauffer        http://www.geocities.com/stauffer_james/
Are you good? Take the test at http://www.livingwaters.com/good/

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: How to use log4j (log4j.xml) and Maven and Junit??

Posted by Jacob Kjome <ho...@visi.com>.
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

Quoting Lisa <ap...@purpleblade.net>:

>
>
>
>
> Jacob Kjome <hoju <at> visi.com> writes:
>
> >
> >
> > 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
> >
> >
>
> 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>
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>




---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: How to use log4j (log4j.xml) and Maven and Junit??

Posted by Lisa <ap...@purpleblade.net>.



Jacob Kjome <hoju <at> visi.com> writes:

> 
> 
> 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
> 
> 

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>





---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: How to use log4j (log4j.xml) and Maven and Junit??

Posted by Jacob Kjome <ho...@visi.com>.
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

At 04:16 PM 1/23/2007, you wrote:
 >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-unsubscribe@logging.apache.org
 >For additional commands, e-mail: log4j-user-help@logging.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: How to use log4j (log4j.xml) and Maven and Junit??

Posted by Jacob Kjome <ho...@visi.com>.
At 07:29 AM 2/24/2007, you wrote:
 >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 ?
 >

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



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: How to use log4j (log4j.xml) and Maven and Junit??

Posted by gerold kathan <ge...@kathan.at>.
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 ?




---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org