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

发布于 2024-07-12 05:35:10 字数 1303 浏览 8 评论 0原文

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

我过去所做的工作是将包含我希望 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。 然而,当我在调试中使用 log4j 以详细模式运行 ant 时,我看到 log4j 选择的是当前工作目录中的 log4j.xml,这不是我想要的。 Log4j 似乎没有使用类路径来解析默认的 log4j.xml。

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜血缘 2024-07-19 05:35:10

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

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

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>
勿忘初心 2024-07-19 05:35:10

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

在一些派生新 JVM 的 ant 任务上,您可以使用指定 log4j.configuration 系统属性。 标签。 但是,对于那些不这样做的人,最好的选择是创建一个类路径条目,其第一个条目是您要使用的 log4j 配置。

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.

仙女 2024-07-19 05:35:10

您可以移动当前工作目录中的 log4j.xml 文件吗?

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

聽兲甴掵 2024-07-19 05:35:10

如果我在运行 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文