How fix ERROR Recursive call to appender log4j2?

Viewed 2140

I migrated my application from log4j to log4j2. I changed every configuration from log4j to log4j2 standards (file name and syntax) but I have this error:

ERROR Recursive call to appender RootConsole

This is my configuration:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>

<Appenders>

    <Console name="RootConsole" target="SYSTEM_OUT">
        <PatternLayout pattern="%d{ISO8601} %c{1} [%t] %p - %m%n"/>
    </Console>

    <RollingFile name="IntegrationTestFile" fileName="it_test.log"
                 filePattern="integration_test.log.%i">
        <PatternLayout pattern="%d{ISO8601} %c{1} [%t] %p - %m%n"/>
        <Policies>
            <SizeBasedTriggeringPolicy size="100 KB"/>
        </Policies>
        <DefaultRolloverStrategy max="1"/>
    </RollingFile>

</Appenders>

<Loggers>

    <Root level="DEBUG">
        <AppenderRef ref="RootConsole"/>
        <AppenderRef ref="IntegrationTestFile"/>
    </Root>

</Loggers>

</Configuration>

This error appears when I exclude one of two module "pl.package.x" or in the same way "pl.package.y":

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>

<Appenders>

    <Console name="RootConsole" target="SYSTEM_OUT">
        <PatternLayout pattern="%d{ISO8601} %c{1} [%t] %p - %m%n"/>
    </Console>

    <RollingFile name="IntegrationTestFile" fileName="it_test.log"
                 filePattern="integration_test.log.%i">
        <PatternLayout pattern="%d{ISO8601} %c{1} [%t] %p - %m%n"/>
        <Policies>
            <SizeBasedTriggeringPolicy size="100 KB"/>
        </Policies>
        <DefaultRolloverStrategy max="1"/>
    </RollingFile>

</Appenders>

<Loggers>

    <Root level="DEBUG">
        <AppenderRef ref="RootConsole"/>
        <AppenderRef ref="IntegrationTestFile"/>
    </Root>

    <!-- Logger which I added -->
    <Logger name="pl.package.x" level="DEBUG" additivity="false">
        <AppenderRef ref="RootConsole"/>
        <AppenderRef ref="IntegrationTestFile"/>
    </Logger>

</Loggers>

</Configuration>

This logger catches everything from "pl.package.x" package and next append it to "RootConsole" and "IntegrationTestFile", the same as root logger. So - if "pl.package.x" or "pl.package.y" is catching by another logger and didn't send events to root logger (additivity="false") everything working correctly - if not I have "ERROR Recursive call to appended".

Maybe someone knows where can be recursive invocations and how fix it?

1 Answers

The error message is generated by the AppenderControl class when it detects nested calls to the same Appender on a thread. Without this a StackOverflowError generally would occur.

In this case, the ConsoleAppender is the component having the problem. Under normal circumstances you should not see this problem with that Appender. However, if System.out is being redirected to something that then performs logging that could cause this error.

Log4j does not product a stack trace when this error occurs. To diagnose it you would need to either a) use a debugger and stop it in AppenderControl where this error is generated or b) temporarily modify a copy of Log4j to log the stack trace. Once you have that you will be able to see what the cause of the recursion is and then either modify the component causing the problem or modify your configuration to suppress logging for that component.

Related