How can I provide a custom log4j.xml for different ant tasks?

13,557

Solution 1

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>

Solution 2

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.

Share:
13,557
Eddie
Author by

Eddie

Physicist by training, now a programmer by choice. My languages of preference are Java and C, although I am also skilled in C# and many assembly languages, and competent in Perl, general shell scripting, and a handful of other languages I use less often. I can speak basic SQL. I write my HTML by hand and prefer table-free page layout. NOTE: I never do retaliatory downvotes or anything petty like that. I am here to learn as well as to teach. If something I write is worthy of a downvote, please leave a comment to let me know why. If I am incorrect, I want to know! I use Linux by preference, but also develop extensively under Windows. I've developed under various embedded and custom OS's and RTOS's (primarily Microware OS-9 and VxWorks), assorted UNIXes, VAX/VMS, and assorted Windows releases. I've played with RSTS/E and a few other operating systems that are unknown today. My home website is pretty sparse, but will some day be populated. Really.

Updated on July 07, 2022

Comments

  • Eddie
    Eddie almost 2 years

    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?

  • Eddie
    Eddie over 15 years
    I would really prefer to not have to move any files around.
  • Eddie
    Eddie over 15 years
    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.
  • Kevin
    Kevin over 15 years
    It seems the jasper2 task supports the "fork" element, can you set that to true and try using a jvmarg?
  • matt b
    matt b over 15 years
    use the -verbose and -debug arguments when starting ant, perhaps the extra info will be helpful on why it fails
  • Eddie
    Eddie over 15 years
    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.