java 如何为不同的 ant 任务提供自定义 log4j.xml?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/481808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:35:06  来源:igfitidea点击:

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

javaantlog4jtaskdef

提问by Eddie

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.

我有一个构建文件,作为构建过程的一部分,它依赖于多个 taskdef。这些 taskdef 项目(例如,webdoclet 和 jasper2)使用 log4j 作为记录器。理想情况下,我希望能够为每个文件提供不同的 log4j 配置文件,但至少,我希望能够指定使用哪个 log4j 配置文件。

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:

我以前所做的工作是将包含我希望 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>

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.

我已经验证,事实上,“Build”目录位于类路径的前面,并且该目录中存在一个 log4j.xml。但是,当我在调试中使用 log4j 以详细模式运行 ant 时,我看到 log4j 正在选择当前工作目录中的 log4j.xml ,这不是我想要的。Log4j 似乎没有使用类路径来解析默认的 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.

我正在使用 log4j 1.2.8(嵌入在更大的框架中,所以我无法升级它)并且其中一些 taskdef 似乎依赖于 commons-logging 1.0.3。构建过程使用 Sun Java 5。

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

如果我set ANT_OPTS=-Dlog4j.configuration=Build/log4j.xml在运行 ant 之前,taskdefs 会正确加载所需的 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?

将“Bu​​ild”目录放在用于工作的 taskdef 的类路径的前面。不知道有什么变化 如何恢复我可以控制的行为——在 build.xml 中——哪个 log4j 配置文件用于给定的任务?除了设置 ANT_OPTS 和重新排列文件以将应用程序的 log4j.xml 移出当前工作目录以使 log4j 找到正确的 log4j.xml 文件之外,还有其他方法吗?

回答by Peter Hilton

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

我可以使用相对文件 URL 指定配置文件,sysproperty如下所示:

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

回答by Kevin

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.

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

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.

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

回答by James Van Huis

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

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

Have you tried setting this property using sysproperty?

您是否尝试过使用 sysproperty 设置此属性?

Should look something like this:

应该是这样的:

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

回答by James A. N. Stauffer

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

你能移动当前工作目录中的 log4j.xml 文件吗?