how to fix Unable to invoke factory method in class org.apache.logging.log4j.core.appender.RollingFileAppender for element RollingFile?

24,739

Solution 1

In my case, it was a permissions issue with the file path being logged to. Changing the permissions fixed it for me

Solution 2

Faced the same issue but this is not an answer to the question though someone might find this helpful.

In my case it was a problem with null system property variable. My logging config file uses some system property variables set in main class, error caused due to a misspelled property name in xml.

System property: hostAddress

System.setProperty("hostAddress", InetAddress.getLocalHost().getHostAddress().replaceAll("\\.", "_"));

Misspelled in config file as: hostAdddress

${log-file-path}/auth-demo-app-${sys:hostAdddress}-${date:yyyy-MM-dd}.log

Correcting this solved the issue.

Share:
24,739
Ajith Deivam
Author by

Ajith Deivam

Failure will never overtake me if my determination to succeed is strong enough. ...

Updated on July 09, 2022

Comments

  • Ajith Deivam
    Ajith Deivam almost 2 years

    I am using spring boot(2.2.0.RELEASE) configure with log4j2. While deploy war in tomcat getting some run time exception.I have set system property(logging name) then that name mentioned in log4j properties.

    Runtime Error :

    ERROR Unable to invoke factory method in class org.apache.logging.log4j.core.appender.RollingFileAppender for element RollingFile: java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.RollingFileAppender java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.RollingFileAppender
    

    Log4j.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="INFO">
        <Properties>
            <Property name="LOG_PATTERN">%d{yyyy-MM-dd'@'HH:mm:ss}|[%t]|[AM]|%p|%X{Slf4jMDCFilter.UUID}|%c{1}()
                %M(%L) %m%n</Property>
            <Property name="APP_LOG_ROOT"
                value="logs/${sys:finlog}">
            </Property>
        </Properties>
        <Appenders>
            <RollingFile name="errorlog"
                fileName="${APP_LOG_ROOT}/error.log"
                filePattern="${APP_LOG_ROOT}/ErrorLog-%d{yyyy-MM-dd}-%i.log">
                <LevelRangeFilter minLevel="ERROR" maxLevel="ERROR"
                    onMatch="ACCEPT" onMismatch="DENY" />
                <PatternLayout pattern="${LOG_PATTERN}" />
                <Policies>
                    <SizeBasedTriggeringPolicy size="19500KB" />
                </Policies>
            </RollingFile>
            <RollingFile name="debugLog"
                fileName="${APP_LOG_ROOT}/debug.log"
                filePattern="${APP_LOG_ROOT}/DebugLog-%d{yyyy-MM-dd}-%i.log">
                <LevelRangeFilter minLevel="DEBUG" maxLevel="DEBUG"
                    onMatch="ACCEPT" onMismatch="DENY" />
                <PatternLayout pattern="${LOG_PATTERN}" />
                <Policies>
                    <SizeBasedTriggeringPolicy size="19500KB" />
                </Policies>
            </RollingFile>
    
            <RollingFile name="infoLog"
                fileName="${APP_LOG_ROOT}/info.log"
                filePattern="${APP_LOG_ROOT}/InfoLog-%d{yyyy-MM-dd}-%i.log">
                <LevelRangeFilter minLevel="INFO" maxLevel="INFO"
                    onMatch="ACCEPT" onMismatch="DENY" />
                <PatternLayout pattern="${LOG_PATTERN}" />
                <Policies>
                    <SizeBasedTriggeringPolicy size="19500KB" />
                </Policies>
            </RollingFile>
    
            <RollingFile name="warnLog"
                fileName="${APP_LOG_ROOT}/warn.log"
                filePattern="${APP_LOG_ROOT}/WarnLog-%d{yyyy-MM-dd}-%i.log">
                <LevelRangeFilter minLevel="WARN" maxLevel="WARN"
                    onMatch="ACCEPT" onMismatch="DENY" />
                <PatternLayout pattern="${LOG_PATTERN}" />
                <Policies>
                    <SizeBasedTriggeringPolicy size="19500KB" />
                </Policies>
                <DefaultRolloverStrategy max="10" />
            </RollingFile>
        </Appenders>
        <Loggers>
            <Logger name="com.demo.engine" additivity="false"
                level="debug">
                <AppenderRef ref="errorlog" />
                <AppenderRef ref="debugLog" />
                <AppenderRef ref="infoLog" />
                <AppenderRef ref="warnLog" />
            </Logger>
            <Logger name="org.springframework" additivity="false"
                level="error">
                <AppenderRef ref="springLog" />
                <AppenderRef ref="console" />
            </Logger>
            <Root level="DEBUG">        
            </Root>
        </Loggers>
    </Configuration>
    

    Main method : In that below main method i have getting context name using class loader then i put the name using system properties.

    public class Main extends SpringBootServletInitializer {
        static {
            String currentpath = "";
            String logAppContext = "";
            if (Thread.currentThread().getContextClassLoader().getResource("") != null) {
                currentpath = Thread.currentThread().getContextClassLoader().getResource("").toString().replace("%23", "#")
                        .replace("file:/", "");
                logAppContext = currentpath.isEmpty() ? logAppContext
                        : Paths.get(currentpath).getParent().getParent().getFileName().toString();
                System.setProperty("finlog", logAppContext);
                System.setProperty("context", logAppContext);
            }
        }
    
        public static void main(String[] args) {
            SpringApplication.run(Main.class, args);
        }
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(Main.class);
        }
    }