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

3

11

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);
    }
}
Underfeed answered 4/3, 2020 at 13:18 Comment(3)
Can you change status="INFO" to status="DEBUG" on the configuration element and also add the output of that to your question?Accentor
@ rgoers I have changed status info to debug but its still same!Underfeed
Did you add the debug output generated to your question? I need to see that to determine what is happening. Just enabling debug isn’t expected to fix the problem.Accentor
T
11

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

Trend answered 19/6, 2020 at 20:45 Comment(0)
R
1

Issue for me:

    <RollingFile
            name="RollingFile"
            fileName="${log.dir}/le-${app}.log"
            filePattern="${log.dir}/le-${app}-%d{yyyy-MM-dd}-%i.log.gz"
            ignoreExceptions="false">
       ...
    </RollingFile>

Fix: just run a chmod on log.dir.

Roche answered 27/10, 2021 at 18:48 Comment(0)
F
0

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.

Fortnight answered 20/10, 2020 at 6:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.