从战争外部加载Log4j.xml

时间:2010-10-26 13:54:49

标签: log4j

在我的应用程序中,我使用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: Invalid 'log4jConfigLocation' parameter: class path resource [/log4j.xml] cannot be resolved to URL because it does not exist

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

6 个答案:

答案 0 :(得分:10)

请记住在路径前面添加“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>

答案 1 :(得分:6)

我有同样的例外。

我使用过这段代码:

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

答案 2 :(得分:5)

您可以像这样简单地声明log4j配置的位置:

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

答案 3 :(得分:0)

您可以使用:

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

log4jConfigLocation 标记内。

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

答案 4 :(得分:0)

您可以在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环境,以double&#34; /&#34;开头。喜欢&#34; file://&#34;。对于带有sigle&#34; /&#34;的linux环境喜欢&#34;文件:/&#34;

答案 5 :(得分:0)

的语法略有不同:使用log4jConfiguration param。 请参考以下答案: @rgoers @Gowtham