Search code examples
javatomcatlog4jlog4j2tomcat9

tomcat error configuration filter got null from web.xml


I have configured some filters (given below) in web.xml of the tomcat server which were working fine before when we are on log4j1.x.

<filter>
    <filter-name>newsession</filter-name>
    <display-name>newsession</display-name>
    <description>newsession</description>
    <filter-class>com.demo.custom.filter.NewSession</filter-class>
    <init-param>
        <param-name>DomainName</param-name>
        <param-value>.testlab.com</param-value>
    </init-param>
    <init-param>
        <param-name>DomConfigFile</param-name>
        <param-value>/opt/tomcat/webapp/demoapp/WEB-INF/classes/NAM_log4j.xml</param-value>
    </init-param>
    <init-param>
        <param-name>customPropFile</param-name>
        <param-value>/opt/tomcat/webapp/demoapp/WEB-INF/classes/custom_resources.properties</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>newsession</filter-name>
    <url-pattern>/idff/sso</url-pattern>
</filter-mapping>

But now when I have moved from log4j1.x to log4j2.x after that when we are starting the tomcat service we are getting the below error in the log. If I placed the "log4j-1.2.15.jar" file again it is working fine. But now we are on log4j2.x so we cannot use the old log4j1.x.

ERROR com.demo.custom.filter.NewSession - No transformation given
ERROR com.demo.custom.filter.NewSession - domConfigFile got null from web.xml, setting hardcoded value
ERROR com.demo.custom.filter.NewSession - custom_resources got null from web.xml, setting hardcoded value
main ERROR Error processing element category ([Configuration: null]): CLASS_NOT_FOUND
main ERROR Unknown object "root" of type org.apache.logging.log4j.core.config.LoggerConfig is ignored: try nesting it inside one of: ["Appenders", "Loggers", "Properties", "Scripts", "CustomLevels"]

My Java code is given below, I have pasted only the code where I am trying to initialize the servlet filters.

package com.demo.custom.filter;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

@WebFilter({ "/NewSession" })
public class NewSession implements Filter {
    static class FilteredRequest extends HttpServletRequestWrapper {
        public FilteredRequest(ServletRequest request) {
            super((HttpServletRequest)request);
        }

        public String getParameter(String paramName) {
            String value = super.getParameter(paramName);
            *********************************************
            return value;
        }

        public static String decryptValue(String value){
            String decryptvalue = null;
            ********************************************
            return decryptvalue;
        }
    }

    private static String domainName = null;
    private static String domConfigFile=null;
    private static Logger myLogger = LogManager.getLogger(NewSession.class);
    private String custom_resources;

    public void init(FilterConfig fConfig) throws ServletException {
        try{
            domainName = fConfig.getInitParameter("DomainName");
            domConfigFile=fConfig.getInitParameter("DomConfigFile");
            custom_resources=fConfig.getInitParameter("customPropFile");
        }catch(Exception e){
            myLogger.error(e.getMessage());
        }
        if(domainName == null){
            domainName = ".testlab.com";
        }
        if(domConfigFile==null){
            myLogger.error("domConfigFile got null from web.xml, setting hardcoded value");
            domConfigFile = "/opt/tomcat/webapp/demoapp/WEB-INF/classes/NAM_log4j.xml";
        }
        if(custom_resources==null){
            myLogger.error("custom_resources got null from web.xml, setting hardcoded value");
            nidp_custom_resources = "/opt/tomcat/webapp/demoapp/WEB-INF/classes/custom_resources.properties";
        }
    }
}

Please help me on this. I am using tomcat 9 and log4j v2.17.1.


Solution

  • My issue is resolved by doing the below changes TEST_log4j.xml file as given below.

    <Configuration>
        <Appenders>
            <Console name="STDOUT" target="SYSTEM_OUT">
                <PatternLayout pattern="%d{MM/dd HH:mm:ss} %-5p %30.30c %x - %m\n"/>
            </Console>
            <RollingFile name="RollingFile" fileName="/opt/tomcat/logs/MyCustomClassLogs.log" filePattern="/opt/tomcat/logs/MyCustomClassLogs.log-%i">
                <PatternLayout>
                    <pattern>%d{MM/dd HH:mm:ss} %-5p %30.30c %x - %m\n</pattern>
                </PatternLayout>
                <Policies>
                    <SizeBasedTriggeringPolicy size="5 MB" />
                </Policies>
                <DefaultRolloverStrategy max="5"/>
            </RollingFile>
            <RollingFile name="ResetClass" fileName="/opt/tomcat/logs/resetTrace.log" filePattern="/opt/tomcat/logs/resetTrace.log-%i">
                <PatternLayout>
                    <pattern>%d{MM/dd HH:mm:ss} %-5p %30.30c %x - %m\n</pattern>
                </PatternLayout>
                <Policies>
                    <SizeBasedTriggeringPolicy size="5 MB" />
                </Policies>
                <DefaultRolloverStrategy max="5"/>
            </RollingFile>
        </Appenders>
        <Loggers>
            <Logger name="com.demo.custom.filter.ResetClass" level="TRACE">
                <AppenderRef ref="ResetClass"/>
            </Logger>
            <Logger name="com.demo.custom.test.MyCustomClass" level="TRACE">
                <AppenderRef ref="RollingFile"/>
            </Logger>
            <Root level="error">
                <AppenderRef ref="STDOUT"/>
            </Root>
        </Loggers>
    </Configuration>