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?
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With