0

My Current web-application is using Servlet 4.0 version and I am trying disable auto-initialization for log4j2 and below is my web.xml I have followed Official document

<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>

disable auto-initialization is not working with above web.xml

I have also tried keeping only below but no luck

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

please correct me if there is anything wrong with above web.xml configuration your help is apricated !!

suman
  • 1
  • 2
  • How can you tell whether auto-initialization is disabled or not? Your `web.xml` contains the definitions of a `Log4jServletContextListener` and `Log4jServletFilter`, which perform the same job as the disabled `Log4jServletContainerInitializer`. – Piotr P. Karwasz May 24 '21 at 16:24

1 Answers1

0

I can confirm auto-initialization is not disabled from server.log file Because , I am having a listener that invokes during server startup , I having some debug statements in the listener which are printing before log4j initialization, hence log4j is initializing first before my listener start . This flow is working as expected with log4j 1.2.17 version

I am setting some variables in the listener which I want to use in log4j2.xml . As log4j is initialization is happening before my listener and variables are not resolving

suman
  • 1
  • 2
  • The issue got resolved, There was couple of other log4j2.xml files available in the project and log4j library is looking to resolve those files for the variables rather than what I am using , after removing these redundant files it starting working – suman May 31 '21 at 06:48