log4j2-找到配置文件 [英] log4j2 - configuration file found

查看:133
本文介绍了log4j2-找到配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是servlet 2.5,我按照说明设置了侦听器和过滤器,但是我的webapp仍然抱怨找不到配置文件.我的侦听器,上下文参数,过滤器和过滤器映射都定义为web.xml中它们各自位置的第一个值.我确保log4j-web-2.2.jar也位于我的类路径中.还有其他选择可以从Spring bean加载它们吗?我想基于catalina.properties中的值加载其他配置文件. 有人可以请教吗?

I am using servlet 2.5 and i followed the instructions to set up the listeners and filters, but still my webapp complains that the configuration file cannot be found. My listener, context param,filter and filter mappings are all defined as the first values in their respective locations in my web.xml. I made sure log4j-web-2.2.jar is in my classpath as well. Are there other options to load them from a spring bean? I would like to load a different config file based on a value in catalina.properties. Can somebody please advice?

 <listener>
    <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
</listener>
<context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>classpath:*${sys:log4j2.xml.name}</param-value>

<filter>
    <filter-name>log4jServletFilter</filter-name>
    <filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>log4jServletFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>ERROR</dispatcher>

</filter-mapping>

推荐答案

经过反复的摸索,我了解到使用classpath表示法不能使用系统变量.因此,我采用了以下方法.基本上,这使我们可以根据环境提供不同的文件名.该变量必须在catalina.properties中指定.

After a lot of fiddling, i learned that with the classpath notation we cannot use system variables. So i went with the following approach. This basically allows us to provide a different file name based on our environment. This variable has to be specified in catalina.properties.

<context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>/WEB-INF/config/${sys:log4j.fileName}</param-value>

这篇关于log4j2-找到配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆