怀人 2020-08-23 14:20 采纳率: 25%
浏览 428

ssm,web.xml中,手动指定log4j2的配置文件不生效?

求助,大佬咩走哇。。。

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的自动加载,是可以正常使用的。

感谢读完,大佬,带带我。。。

  • 写回答

1条回答 默认 最新

  • threenewbee 2020-08-24 09:09
    关注

    派生 SpringJUnit4ClassRunner,在静态构造里
    Log4jConfigurer.initLogging("配置文件路径");

    评论

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组