log4j2 auto-initialization disable is not working with isLog4jAutoInitializationDisabled <context-parm>

430 Views Asked by At

emphasized textI'm working on migration of log4j version from 1.2.17 to Log4j2 , as part of migration I have chosen bridge option as per the guideliness , while migrating log4j.xml to log4j2.xml for RollingFile we are having properties to substitute for file creation as below

     <RollingFile name="file"
        fileName="${log.directory}server.log.${log.id}server.log"
        filePattern="${log.directory}server.log.${log.id}server.log">
        <PatternLayout pattern="%d{ISO8601} %-5p (%t %X{RequestId} %X{CallerId}) [%c{1}] %m%n" />
        <Policies>
            <SizeBasedTriggeringPolicy size="5000 KB" />
        </Policies>
        <DefaultRolloverStrategy max="5" />
    </RollingFile>
  

And these variables resolved after execution of one of custom (com.MyCustomListener) written during startup , This flow is working as expected with log4j 1.2.17 . With log4j2 as per the Specification provided by apache auto-initialization of log4j2 happens during server startup automatically , due to this auto-initialization at the being of server startup my listener which resolves variables is executing after initialization of log4j as a result variables are not resolving, I am using servlet version 4.0 I tried to disable auto-initialization by adding below beginning of my web.xml

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

But still this is not helping me , log4j2 initialization is happening before my execution I want my execution first before log4j2 initialization.

            <?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" 
                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>MyApplication</display-name>
                
                 <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>
                
                <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.MyCustomListener</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>
                
                <!-- welcome file list-->
                <welcome-file-list>
                    <welcome-file>index.html</welcome-file>
                </welcome-file-list>
                
            </web-app>

I have enabled TRACE logs for log4j2 , From the logs I have observed that log4j is initializing before my custom listener(com.MyCustomListener)

*2021-05-19 05:11:49,314 INFO [stdout] (ServerService Thread Pool -- 71) 2021-05-19 05:11:49,314 ServerService Thread Pool -- 71 TRACE TypeConverterRegistry initializing. 2021-05-19 05:11:49,314 INFO [stdout] (ServerService Thread Pool -- 71) 2021-05-19 05:11:49,314 ServerService Thread Pool -- 71 DEBUG PluginManager 'TypeConverter' found 26 plugins 2021-05-19 05:11:49,330 INFO [stdout] (ServerService Thread Pool -- 71) 2021-05-19 05:11:49,330 ServerService Thread Pool -- 71 DEBUG PatternLayout$Builder(pattern="%d{ISO8601} %-5p (%t) [%c{1}] %m%n", PatternSelector=null, Configuration(vfs:/C:/SL3/CPS_EMR_ALL/jboss/standalone/deployments/PracticeEAR.ear/lib/Practice.jar/log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", disableAnsi="null", noConsoleNoAnsi="null", header="null", footer="null") 2021-05-19 05:11:49,330 INFO [stdout] (ServerService Thread Pool -- 71) 2021-05-19 05:11:49,330 ServerService Thread Pool -- 71 DEBUG PluginManager 'Converter' found 46 plugins 2021-05-19 05:11:49,330 INFO [stdout] (ServerService Thread Pool -- 71) 2021-05-19 05:11:49,330 ServerService Thread Pool -- 71 DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.ConsoleAppender]. 2021-05-19 05:11:49,345 INFO [stdout] (ServerService Thread Pool -- 71) 2021-05-19 05:11:49,345 ServerService Thread Pool -- 71 DEBUG ConsoleAppender$Builder(target="SYSTEM_OUT", follow="null", direct="null", bufferedIo="null", bufferSize="null", immediateFlush="null", ignoreExceptions="null", PatternLayout(%d{ISO8601} %-5p (%t) [%c{1}] %m%n), name="console", Configuration(vfs:/C:/SL3/CPS_EMR_ALL/jboss/standalone/deployments/PracticeEAR.ear/lib/Practice.jar/log4j2.xml), Filter=null, ={}) 2021-05-19 05:11:49,345 INFO [stdout] (ServerService Thread Pool -- 71) 2021-05-19 05:11:49,345 ServerService Thread Pool -- 71 DEBUG Starting OutputStreamManager SYSTEM_OUT.false.false 2021-05-19 05:11:49,345 INFO [stdout] (ServerService Thread Pool -- 71) 2021-05-19 05:11:49,345 ServerService Thread Pool -- 71 DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout]. 2021-05-19 05:11:49,345 INFO [stdout] (ServerService Thread Pool -- 71) 2021-05-19 05:11:49,345 ServerService Thread Pool -- 71 DEBUG PatternLayout$Builder(pattern="%d{ISO8601} %-5p (%t %X{RequestId} %X{CallerId}) [%c{1}] %m%n", PatternSelector=null, Configuration(vfs:/C:/SL3/CPS_EMR_ALL/jboss/standalone/deployments/PracticeEAR.ear/lib/Practice.jar/log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", disableAnsi="null", noConsoleNoAnsi="null", header="null", footer="null") 2021-05-19 05:11:49,580 INFO [stdout] (ServerService Thread Pool -- 71) 2021-05-19T05:11:49,580 INFO (ServerService Thread Pool -- 71) [MyCustomListner] ********************** Entry to MyCustomListner class ********************** 2021-05-19 05:11:49,611 INFO [stdout] (ServerService Thread Pool -- 71) 2021-05-19T05:11:49,611 ERROR (ServerService Thread Pool -- 71) [MyCustomListner] ********************** Exit from MyCustomListner class ***********************

Your help is apricated

0

There are 0 best solutions below