Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
    }
}
like image 353
Ajith Deivam Avatar asked Mar 03 '23 17:03

Ajith Deivam


2 Answers

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

like image 151
James Dube Avatar answered Mar 05 '23 11:03

James Dube


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.

like image 44
benjamin c Avatar answered Mar 05 '23 10:03

benjamin c