log4j2 - configuration file found

2019-08-29 10:18发布

问题:

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>

回答1:

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>



回答2:

If your config file is named log4j2.xml and is located in the WEB-INF/ directory then you don't need to specify the log4jConfiguration context parameter in your web.xml.



标签: spring log4j2