0

我当前的网络应用程序使用的是 Servlet 4.0 版本,我正在尝试禁用 log4j2 的自动初始化,下面是我遵循的 web.xml官方文档

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    id="WebApp_ID" version="2.5">
    
    <display-name>CentricityPracticeWS</display-name>
    
     <listener>
        <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
    </listener>
 
    <filter>
        <filter-name>CentricityPracticeWS</filter-name>
        <filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CentricityPracticeWS</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>
    
     <context-param>
        <param-name>isLog4jContextSelectorNamed</param-name>
        <param-value>true</param-value>
    </context-param>

    <context-param>
        <param-name>log4jConfiguration</param-name>
        <param-value>file://C://jboss//standalone//deployments//CentricityPracticeEAR.ear//init//log4j2.xml</param-value>
    </context-param>
    
        <context-param>
        <param-name>log4jContextName</param-name>
        <param-value>CentricityPracticeWS</param-value>
    </context-param>

    
    <context-param>
      <param-name>isLog4jAutoInitializationDisabled</param-name>
      <param-value>true</param-value>
    </context-param>

    <!-- init properties shared by entire application -->
    <context-param>
        <param-name>earDeploymentDescriptorPath</param-name>
        <param-value>application.xml</param-value>
    </context-param>
    <listener>
        <listener-class>com.CustomListener</listener-class>
    </listener>
    
    <!-- bootstrap Log4j -->
    <!-- Log4j refresh interval -->
    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>60000</param-value>
    </context-param>
    <context-param>
        <param-name>log4jExposeWebAppRoot</param-name>
        <param-value>false</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    
    <!-- bootstrap Spring -->
    <context-param>
        <param-name>parentContextKey</param-name>
        <param-value>centricity.practice</param-value>
    </context-param>
    <context-param>
        <param-name>locatorFactorySelector</param-name>
        <param-value>/beanRefFactory.xml</param-value>
    </context-param> 
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/context-web.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- welcome file list-->
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    
</web-app>

禁用自动初始化不适用于上面的 web.xml

我也试过只保持在下面但没有运气

<context-param>
    <param-name>isLog4jAutoInitializationDisabled</param-name>
    <param-value>true</param-value>
</context-param>

如果上面的 web.xml 配置有任何问题,请纠正我,您的帮助很重要!

4

1 回答 1

0

我可以确认没有从 server.log 文件中禁用自动初始化因为,我有一个在服务器启动期间调用的侦听器,我在侦听器中有一些调试语句在 log4j 初始化之前打印,因此 log4j 在我的侦听器之前先初始化开始 。此流程在 log4j 1.2.17 版本中按预期工作

我在监听器中设置了一些我想在 log4j2.xml 中使用的变量。由于 log4j 是在我的侦听器和变量未解析之前进行初始化

于 2021-05-27T07:27:15.337 回答