从战争外部加载 Log4j.xml

发布于 2024-09-29 07:26:41 字数 1316 浏览 0 评论 0原文

在我的应用程序中,我使用 Log4j 进行日志记录。目前,我将 log4j.xml 放置在 WEB-INF/classes 中。 以下是我用来加载 log4j.xml 文件的配置。

<!-- language: xml -->

    <context-param>
          <param-name>log4jConfigLocation</param-name>
         <param-value>/WEB-INF/classes/log4j.xml</param-value>
    </context-param>

    <listener>
       <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener> 

现在我需要将 log4j.xml 文件放在 war 文件之外。该位置很可能是 JBOSS_HOME/server/default/deploy/settings。在设置目录中,我需要放置 log4j.xml。

我尝试通过编辑 run.bat 设置 jboss 类路径来加载它,如下所示 设置 JBOSS_CLASSPATH=%RUN_CLASSPATH%;%JBOSS_HOME%\server\default\deploy\settings 我在下面的 web.xml 中使用了它,

<!-- language: xml -->

    <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>classpath:/log4j.xml</param-value>  
    </context-param>

    <listener>
            <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener> 

但它在部署应用程序时抛出异常。例外的是 java.lang.IllegalArgumentException:无效的“log4jConfigLocation”参数:类路径资源[/log4j.xml]无法解析为URL,因为它不存在

现在我的问题是如何加载它。

In my application iam using Log4j for logging.Presently I am placing log4j.xml in WEB-INF/classes.
Below are the configurations i am using to load log4j.xml file.

<!-- language: xml -->

    <context-param>
          <param-name>log4jConfigLocation</param-name>
         <param-value>/WEB-INF/classes/log4j.xml</param-value>
    </context-param>

    <listener>
       <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener> 

Now I need to place log4j.xml file outside of my war file. The location will be most likely JBOSS_HOME/server/default/deploy/settings. In settings directory i need to place my log4j.xml.

I tried to load it by setting jboss class path by editing run.bat as follows
set JBOSS_CLASSPATH=%RUN_CLASSPATH%;%JBOSS_HOME%\server\default\deploy\settings
and i used below in web.xml

<!-- language: xml -->

    <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>classpath:/log4j.xml</param-value>  
    </context-param>

    <listener>
            <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener> 

But it throwing exception while deploying application. Exception is
java.lang.IllegalArgumentException: Invalid 'log4jConfigLocation' parameter: class path resource [/log4j.xml] cannot be resolved to URL because it does not exist

Now my question is how can I load it.

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

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

发布评论

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

评论(6

心的憧憬 2024-10-06 07:26:41

请记住在路径前面添加“file://”,否则它将在 webapp 文件夹内搜索。

下面的例子对我有用。

<web-app>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>file://${MY_ENV_VAR}log4j.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
</web-app>

Remember to add "file://" in front of the path, or else it will search inside the webapp folder.

The following example works for me.

<web-app>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>file://${MY_ENV_VAR}log4j.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
</web-app>
抱猫软卧 2024-10-06 07:26:41

我也有同样的例外。

我使用了这段代码:

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

当我执行时,这个异常消失了:

mvn 全新安装

I had the same exception.

I had used this code:

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

This exception disappeared when I executed:

mvn clean install

小猫一只 2024-10-06 07:26:41

您可以简单地声明 log4j 配置的位置,如下所示:

<context-param>
      <param-name>log4jConfigLocation</param-name>
     <param-value>${JBOSS_HOME}/server/default/deploy/settings/log4j.xml</param-value>
</context-param>

You can simply declare location of log4j configuration like this:

<context-param>
      <param-name>log4jConfigLocation</param-name>
     <param-value>${JBOSS_HOME}/server/default/deploy/settings/log4j.xml</param-value>
</context-param>
等风也等你 2024-10-06 07:26:41

您可以使用:

  • file:///${MY_ENV_VAR}/log4j.xml
  • file:///${JBOSS_HOME}/server/default/deploy/settings/log4j.xml

在您的 log4jConfigLocation 标记中。

如果路径是绝对路径(在webapp文件夹之外),请注意添加file:///

You can use:

  • file:///${MY_ENV_VAR}/log4j.xml
  • file:///${JBOSS_HOME}/server/default/deploy/settings/log4j.xml

Inside in your log4jConfigLocation tag.

Pay attention to add file:/// if the path is absolute (outside the webapp folder)

笑着哭最痛 2024-10-06 07:26:41

您可以在 web.xml 文件中这样设置:

    <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>file://${JBOSS_HOME}/domain/configuration/log4j.xml</param-value>
        </context-param>

  <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

注意:对于 Windows 环境,以双“/”开头,如“file://”。对于带有单个“/”的Linux环境,例如“file:/”。

You can set it like this in the web.xml file:

    <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>file://${JBOSS_HOME}/domain/configuration/log4j.xml</param-value>
        </context-param>

  <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

Note: for windows environment start with double "/" like "file://". For linux environment with sigle "/" like "file:/" .

小情绪 2024-10-06 07:26:41

的语法有点不同:使用 <代码>log4jConfiguration 参数。
参考这些答案:
@rgoers
@Gowtham

The syntax for is a little different: uses log4jConfiguration param.
Refer to these answers:
@rgoers
@Gowtham

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