2

I've been trying to get logging with Apache log4j to appear correctly in Eclipse's console when running in Ant and am having no luck. I have a working log4j.xml configuration that has a basic root logger, with a debug priority and a ConsoleAppender. I have verified that the logger works because if I remove the log4j.xml from the project, the Ant target complains about it missing a log4j configuration. If I switch the appender to a file appender, all of the output properly goes to the file. Switching it back to the console, I get nothing to the Eclipse console except the standard Ant output.

My JUnit tests, however, output log4j messages but I believe that is because I run them in batch and have those processes forked.

If I run the exact same Ant script on the command line with no changes, I get the log4j output I expect interlaced with the Ant output. I am running Ant 1.7.1 in Eclipse Helios on my Mac 10.6 Intel 64 machine. I have verified this is also an issue on a Windows XP machine running Helios. I have tried setting "follow" to true in my console appender configuration and this has not helped.

I can provide code snippets, but like I've mentioned, I've proven that log4j is configured correctly and the log4j.xml is being picked up on the classpath and logging is being performed and outputted. It has something to do with how Ant integrates with Eclipse - anyone else experience this? Should this be something I submit to the Eclipse project?

Aaron Douglas
  • 2,494
  • 1
  • 17
  • 14
  • See also: http://stackoverflow.com/questions/3501355/log4j-output-not-displayed-in-eclipse-console – martin clayton Jan 07 '11 at 18:15
  • This is not a direct answer, and I'm not sure, if it's related - but one problem I've noticed with the Eclipse console is, that in contrast to most other consoles, it tends to not print messages until an explicit `flush()` is invoked on System.out (or System.err). So if possible, try a flush() after logging a message (this would not be a permanent solution of course). – Chris Lercher Jan 07 '11 at 22:50
  • @Martin - My JUnit tests output correctly at every level, it only seems to be the custom taskdef that I've defined. I've going to go back and try running this as a task and see what happens instead through my custom Ant task. – Aaron Douglas Jan 12 '11 at 20:42
  • @Chris - your comment has also given me the idea of adding actual System.out.println statements to my code for testing purposes. Right now, I'm only using slf4j/log4j logging. If I don't even see System.out working then I know for sure it's Eclipse wrapping the console with Ant. I'll try flushing at that point too. – Aaron Douglas Jan 12 '11 at 20:44
  • when you come up with the solution, perhaps post something on that other question too? – martin clayton Jan 12 '11 at 20:45

2 Answers2

0

I had a similar issue, Eclipse was not logging the junit messages or log4j. To get the junit log messages adding a formatter made the difference. This seems to pass the log4j messages only once the test case is completed.

<target name="junit" depends="copy_cfg">
        <junit printsummary="on" fork="false" haltonfailure="no">
            <classpath refid="MyProject.classpath" />
            <formatter type="xml" />
            <formatter type="plain" usefile="false" /> <!-- to screen -->
            <batchtest todir="${test.report.dir}">
                <fileset dir="${src.dir}">
                    <include name="**/*Test.java" />
                </fileset>
            </batchtest>
        </junit>
    </target>

The issue with the log4j was because I did not have the config file in the build directory, so after the ANT build process I copy it across to the build path

<!--  copy the config files for junit -->
<target name="copy_cfg" depends="compile">
    <copy file="log4j.xml" todir="${build.dir}" flatten="true"/>
    <copy file="config.txt" todir="${build.dir}" flatten="true"/>
</target>  

Hope this helps someone.

oden
  • 3,461
  • 1
  • 31
  • 33
0

Java tasks executed through ant don't send there standard output and error to the main ant's output.

Have you looked at the output, error and i/o redirectors of the Ant Java task?

Yoni
  • 10,171
  • 9
  • 55
  • 72
  • I somehow solved this a while back and I never updated the question. I believe it was related to the redirectors you've suggested. – Aaron Douglas May 23 '12 at 12:32