tags:

views:

147

answers:

2

What I'm trying to achieve is to have log4j as the only output from my Ant build file.

Getting log4j works pretty much perfectly except that it extends Ants output. So I'm getting Ant's DefaultLogger interspersed with log4j output. Take a VERY simple Ant build file:

<project name="Maintenance_to_Delivery" default="main" basedir=".">
    <target name="main">
         <echo message="test">
    </target>
</project>

And log4j.properties

log4j.rootLogger=ERROR, stdout
log4j.logger.org.apache.tools.ant.Project=INFO
log4j.logger.org.apache.tools.ant.Target=INFO
log4j.logger.org.apache.tools.ant.taskdefs=INFO
log4j.logger.org.apache.tools.ant.taskdefs.Echo=INFO

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss} %-5p %m%n
log4j.appender.stdout.Threshold=INFO

And running this plus the output:

# ant -lib /app/apache-sf/log4j/1.2.16/log4j-1.2.16.jar:. -listener org.apache.tools.ant.listener.Log4jListener

Buildfile: /home/sysswb/working/qa/sep/M2D-new/build.xml
16:36:59 INFO  Build started.

main:
16:37:00 INFO  Target "main" started.
     [echo] TEST
16:37:00 WARN  TEST
16:37:00 INFO  Target "main" finished.

BUILD SUCCESSFUL
Total time: 1 second
16:37:00 INFO  Build finished

so - is it possible to get log4j to completely replace the DefaultLogger output? ...without writing a custom logger? Since the -listener argument registers extra listeners, can whatever listener the DefaultLogger uses be removed/disabled?

I'm just Java 1.6.0_18 and Ant 1.8.0. TIA.

A: 

You can pass log4j.properties in the class path while running the ant.

Check out this, it has much detailed explanation, follow comment by "Kevin", here is the excerpt:

Log4j loads its configuration by looking for the system property log4j.configuration. Failing that, it looks for log4j.properties or log4j.xml on the classpath.

Amit Goyal
log4j works perfectly - the example output in my question shows that. My problem is that it extends the output ... I guess I should add to my question "how do I suppress the DefaultLogger output"
DisabledLeopard
Just for clarification, you don't want DefaultLogger to be used by Ant. Instead, your log4j.properties should be used. Is this the case OR do you want to completely suppress the Ant logging?
Amit Goyal
@Amit I want log4j to completely control the output - Ant should display nothing except what log4j says. log4j works fine - I'm just struggling to remove the default Ant logger part.
DisabledLeopard
A: 

try using the -e switch

ant -lib . -listener org.apache.tools.ant.listener.Log4jListener -e main
Buildfile: D:\install\apache-ant-1.8.0RC1-bin\apache-ant-1.8.0RC1\bin\build.xml
14:29:20 INFO  Build started.

main:
14:29:20 INFO  Target "main" started.
 test
14:29:20 WARN  test
 14:29:20 INFO  Target "main" finished.

BUILD SUCCESSFUL
Total time: 0 seconds
14:29:20 INFO  Build finished.

[echo] test is gone

JoseK
all -e does is remove the adornments. test is still output all that's gone is the "[echo]" - ie the line above "14:29:20 WARN test"
DisabledLeopard
But you're echoing 'test' in the script. so that has to appear. i did mention above that the '[echo] test' is gone.
JoseK