Search code examples
javaspring-bootlog4j2

Cause ERROR Recursive call to appender. Is there a way to solve it?


The following error message has occurred.

ERROR Recursive call to appender file_appender

Log4j2 - AppenderControl.isRecursiveCall()

@PerformanceSensitive
private boolean isRecursiveCall() {
    if (recursive.get() != null) {
        appenderErrorHandlerMessage("Recursive call to appender ");
        return true;
    }
    return false;
}

I even found the code above. Is there any way to solve the error that occurs?

Any help would be greatly appreciated.


I found some issue.

Message "ERROR Recursive call to appender" needs more diagnostic information. - https://issues.apache.org/jira/browse/LOG4J2-2738


I solved the problem.

log4j2.xml

<Appenders>
    <Routing>    
        <Routes>
            <Route>  
                <RolingFile ... >
            </Route>  
        </Routes>
    </Routing>
</Appenders>

Normal logging within appenders could create infinite recursion which Log4j will detect and cause the recursive events to be ignored.

https://logging.apache.org/log4j/log4j-2.2/manual/configuration.html

If the status attribute is set to error than only error messages will be written to the console. This makes troubleshooting configuration errors possible. As an example, if the configuration above is changed to have the status set to error and the logger declaration is:

<logger name="EventLogger" level="info" additivity="false">
  <AppenderRef ref="Routng"/>
</logger>

Solution

  • This error happens when an application tries to log something, the event is routed to an appender, the appender tries to log something and that event is routed back to the same appender. This could end up in an endless loop that causes a stack overflow exception. Instead, Log4j detects this and prevents it. The way to avoid this is to add a logger for the event(s) causing the problem and either disable them or route them to a different appender.