I figured this out this myself, and am posting here in case anyone else has the 
same problem.  I'm sure there are other, simpler, ways to do this, but this 
worked for me, so I'm putting it out there.

If you want your log4j logging output to get sent to your JUnit reports (e.g., 
TEST-package.TestFoo.xml) here is one way that you can configure Loggers 
programatically to do that.

Class being tested = Foo.java
JUnit TestCase class = TestFoo.java

Use the following very generic log4j.xml in your test environment.

<log4j:configuration>
  <appender name="STDOUT"
    class="org.apache.log4j.ConsoleAppender">
    <param name="Threshold" value="warn"/>
    <layout class="org.apache.log4j.PatternLayout">
     <param name="ConversionPattern" value="%c{1} %m [line %L]%n"/>
    </layout>
  </appender>
  <root>
    <level value="warn"/>
    <appender-ref ref="STDOUT"/>
  </root>
</log4j:configuration>

Include the following in your project.xml <unitTest> configuration:

        <resource>
          <directory>src/test/test-conf</directory>
          <includes>
            <include>log4j.xml</include>
          </includes>
        </resource>

All other log4j configuration will be done programatically in your individual 
test classes.

Now, suppose that your Foo.java class has logging statements such as:
log.debug("value of String xyx = " + xyz);

To get the output from these logging statements to appear in your JUnit report 
(TEST-package.TestFoo.xml) generated for the test TestFoo.java, you need the 
following log4j configuration stuff in TestFoo.jar.

A static reference to the logger used inside of Foo.java ...

  private static Logger logOfClassBeingTested = Logger.getLogger(Foo.class);

A global reference (not static) to an appender that will send output to 
wherever JUnit redirects the System.out ....

  private ConsoleAppender appender = null;

Some code inside the TestFoo.setUp() method to add an appender to the logger ...

    logOfClassBeingTested.setLevel(Level.DEBUG);
    PatternLayout layout = new PatternLayout("%c{1} %m [line %L]%n");
    // if JUnit is redirecting STDOUT so will this appender
    appender = new ConsoleAppender(layout); 
    logOfClassBeingTested.addAppender(appender);

Lastly, you need some code in tearDown() to remove the appender (or else it 
gets attached multiple times) ...

    // remove so that we don't end up with multiple appenders
    logOfClassBeingTested.removeAppender(appender);
    appender = null; 



> -----Original Message-----
> From: Mark D. Hansen [mailto:[EMAIL PROTECTED]
> Sent: Thursday, March 10, 2005 9:41 AM
> To: Maven User (E-mail)
> Subject: JUnit report - behavior of System.out vs. Log4J
> 
> 
> I run "maven test" to get the reports of form: 
> TEST-package.TestFoo.xml.
> 
> I've configured a Log4J logger to write debugging info to 
> System.out, but it is not getting redirected to the 
> TEST-package.TestFoo.xml file the same way that 
> System.out.println(...) statements do.  Details below ...
> 
> If TestFoo.java contains the following line:
> 
>       System.out.println("hello world");
> 
> Then, I get this at the end of the report (TEST-package.TestFoo.xml):
> 
>   <system-out><![CDATA[hello world
> ]]></system-out>
> 
> because the JUnit System.out is redirected (as expected) to 
> this report file.
> 
> 
> However, if TestFoo.java contains the following line:
> 
>       log.debug("hello world");
> 
> then there is NOTHING in the report, but on the console, I get:
> 
>     [junit] hello world [line 338]
> 
> Here is how I am configuring the logger in TestFoo.java:
> 
>       logOfTestedClass = Logger.getLogger(Foo.class);
>       logOfTestedClass.setLevel(Level.DEBUG);
>       PatternLayout layout = new PatternLayout("%m [line %L]%n");
>       WriterAppender appender = new WriterAppender(layout, 
> System.out);
>       logOfTestedClass.addAppender(appender);
> 
> So, the Log4j appender is sending the logging output to 
> System.out, but Maven is not sending it to the report file.
> 
> Does anybody know how I can get the desired behavior?  I 
> don't wan't to put System.out.println statements in Foo.java 
> - it seems to me a much better practice to use log statements 
> that can be turned off in production.
> 
> Thanks for any help you can provide.
> 
> -- Mark
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to