代码之家  ›  专栏  ›  技术社区  ›  Eddie

如何为不同的ant任务提供一个定制的log4j.xml?

  •  5
  • Eddie  · 技术社区  · 16 年前

    我以前所做的工作是在类路径的前面放一个包含我希望taskdef使用的log4j.xml的目录。例如:

    <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>
    

    事实上,“Build”目录位于类路径的前面,并且该目录中存在log4j.xml。但是,当我在verbose模式下运行ant时,在debug中使用log4j,我看到log4j选择了 ,这不是我想要的。Log4j似乎没有使用类路径来解析默认的Log4j.xml。

    我使用的是log4j1.2.8(嵌入在一个更大的框架中,因此我无法升级它),其中一些taskdef似乎依赖于commons logging 1.0.3。构建过程使用sunjava5。

    如果我 set ANT_OPTS=-Dlog4j.configuration=Build/log4j.xml 在运行ant之前,taskdefs正确地加载所需的log4j.xml。

    4 回复  |  直到 10 年前
        1
  •  4
  •   Peter Hilton    15 年前

    我可以使用相对文件URL指定配置文件 sysproperty 这样地:

    <java classname="com.lunatech.MainClass">
       <sysproperty key="log4j.configuration" value="file:src/log4j-debug.properties"/>
       <classpath refid="runtime.classpath"/>
    </java>
    
        2
  •  2
  •   Kevin    16 年前

    Log4j通过查找系统属性Log4j.configuration加载其配置。如果没有这样做,它会在类路径上查找log4j.properties或log4j.xml。

    对于一些派生新JVM的ant任务,可以使用<jvmarg/>标记指定log4j.configuration系统属性。但是,对于那些不是最好的选择,您最好创建一个类路径条目,它的第一个条目是您想要使用的log4j配置。

        3
  •  0
  •   James A. N. Stauffer    16 年前

        4
  •  0
  •   James Van Huis    16 年前

    如果在运行ANT之前设置ANT_OPTS=-Dlog4j.configuration=Build/log4j.xml,taskDef将正确加载所需的log4j.xml。

    你试过用sysproperty设置这个属性吗?

    <target name="define.jasper2">
       <sysproperty key="log4j.configuration" value="Build/log4j.xml"/>
       ...
    </target>