views:

3538

answers:

4

I have a build file that as part of the build process relies on several taskdefs. These taskdef items (for example, webdoclet and jasper2) use log4j as a logger. Ideally, I'd like to be able to provide a different log4j configuration file for each, but minimally, I'd like to be able to specify which log4j configuration file is used.

What I did that used to work was to put at the front of the classpath the directory containing the log4j.xml that I wanted the taskdef to use. For example:

  <target name="define.jasper2">
    <path id="jspc.classpath">
      <!-- Put this FIRST so Jasper will find the log4j.xml in the Build directory -->
      <pathelement location="Build"/>
      <fileset dir="${tomcat.libs}">
             ....
      </fileset>
      <pathelement location="${log4j.jar}"/>
      ....
    </path>

    <taskdef name="jasper2" classname="org.apache.jasper.JspC" classpathref="jspc.classpath"/>
  </target>

I've verified that, in fact, the "Build" directory is at the front of the classpath and that a log4j.xml exists in that directory. However, when I run ant in verbose mode with log4j in debug, I see that log4j is choosing the log4j.xml that is in the current working directory, which is not the one I want. Log4j appears to not be using the classpath to resolve the default log4j.xml.

I'm using log4j 1.2.8 (embedded within a larger framework, so I cannot upgrade it) and some of these taskdefs appear to rely on commons-logging 1.0.3. The build process uses Sun Java 5.

If I set ANT_OPTS=-Dlog4j.configuration=Build/log4j.xml before running ant, the taskdefs correctly load the desired log4j.xml.

Putting the "Build" directory at the front of the classpath for the taskdefs used to work. I don't know what changed. How can I restore the behavior where I can control -- within the build.xml -- which log4j configuration file is used for a given task? Is there a way other than setting ANT_OPTS and other than rearranging files to move the application's log4j.xml out of the current working directory to get log4j to find the correct log4j.xml file?

A: 

Can you move the log4j.xml file that is in the current working directory?

James A. N. Stauffer
I would really prefer to not have to move any files around.
Eddie
+1  A: 

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.

On some ant tasks that fork a new JVM, you can specify the log4j.configuration system property with <jvmarg/> tag. However, on those that don't your best bet is to create a classpath entry whose first entry is the log4j configuration you would like to use.

Kevin
Well, I'm doing what you suggest -- I create a classpath entry whose first entry is the (directory containing the) log4j configuration to use. This used to work, but now doesn't. If I put the log4j.xml file on the classpath, it still doesn't work.
Eddie
It seems the jasper2 task supports the "fork" element, can you set that to true and try using a jvmarg?
Kevin
use the -verbose and -debug arguments when starting ant, perhaps the extra info will be helpful on why it fails
matt b
A: 

If I set ANT_OPTS=-Dlog4j.configuration=Build/log4j.xml before running ant, the taskdefs correctly load the desired log4j.xml.

Have you tried setting this property using sysproperty?

Should look something like this:

<target name="define.jasper2">
   <sysproperty key="log4j.configuration" value="Build/log4j.xml"/>
   ...
</target>
James Van Huis
Unfortunately, at least in Ant 1.7.0, you cannot use sysproperty either within taskdef nor standalone in a target. So this doesn't work.
Eddie
+1  A: 

I am able to specify an configuration file using a relative file URL with sysproperty like this:

<java classname="com.lunatech.MainClass">
   <sysproperty key="log4j.configuration" value="file:src/log4j-debug.properties"/>
   <classpath refid="runtime.classpath"/>
</java>
Peter Hilton