求助,大佬咩走哇。。。
1.问题
ssm项目,在web.xml中,通过 log4jConfiguration 参数,手动指定配置文件不生效,log4j2还是会自动读取classpath下第一个符合的log4j2的配置文件。
2.截图
1.web.xml中的配置如下:
<context-param>
<param-name>isLog4jAutoInitializationDisabled</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>file:///D:/log4j2.xml</param-value>
</context-param>
配不配置 isLog4jAutoInitializationDisabled 参数,都没有效果。
2.当上面的 isLog4jAutoInitializationDisabled 参数=true时,配置log4j2的监听、过滤器如下,却报找不到 Log4jServletContextListener 类异常,但在log4j-web包下,明明有这个类。
<listener>
<listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
</listener>
<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>
官网有如下描述:
You must not manually configure the Log4jServletContextListener or Log4jServletFilter in your deployment descriptor (web.xml) or in another initializer or listener in a Servlet 3.0 or newer application unless you disable auto-initialization with isLog4jAutoInitializationDisabled.
我把 isLog4jAutoInitializationDisabled 设为true了,也一样报上文提到的异常,是我少引了什么依赖吗?
另外官网还说:
You must do so in a way that this initialization happens before any other application code (such as Spring Framework startup code) executes.
这个我不知道怎么设置,只是把web.xml中,log4j2的配置,都提到对应部分的最前面了。
3.根据官网的描述,我对log4j2.xml的配置文件,作了如下设置,也都没有效果:
<!-- web.xml中有配置<display-name>,这里依然无效 -->
<property name="LOG_HOME">${sys:catalina.home}/logs/${web:servletContextName}</property>
<!-- 获取web.xml中自定义的<context-param>参数,无效 -->
<property name="LOG_HOME">${sys:catalina.home}/logs/${web:initParam.test}</property>
3.环境
- jdk 1.8
- apache-tomcat-7.0.94
- spring-5.2.0
- log4j-api-2.13.3
- log4j-core-2.13.3
- log4j-web-2.13.3
- web-app的属性如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
4.补充
当前的项目中,web.xml里不做任何设置,直接使用log4j2的自动加载,是可以正常使用的。
感谢读完,大佬,带带我。。。