1

In log4j1 we could pass multiple appenders comma seperated to a logger as:

logger.name = log_level, appender1, appender2, appender3

if one of the appender (say appender1) was having error handler set as OnlyOnceErrorHandler

Then, if the appender1 failed to log output to target, it will instead log the issue to internal log for 1st time only. Next time onwards, it will behave as NOOP.

An example config in log4j1 for ref:

<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
    <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
    <param name="Target" value="System.out"/>
    <param name="Threshold" value="WARN"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d %-5p [%t] %c - %m%n"/>
    </layout>
</appender>

I want to achieve same error handling behaviour in Log4j2.

As per this stackoverflow question log4j2 provides a failoverappender which I thought will help. But i am unable to make sense out of it, meaning I dont think there is an OOTB way in log4j2 to make an appender behave like Consoleappender with OnlyOnceErrorHandler in Log4j1.

The failover appender allows us to call other appenders if the primary appender fails/throws, and allows to retry the primary appender after a configurable interval of seconds. But it does not say it can stop the appender getting called after n number of exceptions.

Can someone help me with the issue? Thanks in advance!

r_goyal
  • 1,117
  • 1
  • 11
  • 20

1 Answers1

0

I have been trying to solve the same issue, and the closest that I have found is the 'ignoreExceptions' parameter on the appender.

  • The OnlyOnceErrorHandler will log errors from the appender in System.err, only the once, and then will carry on.
  • The 'ignoreExceptions' param will log the error to the log4j log, only the once, and then will carry on.
  • So the difference appears to be where the appender error is logged, which we learned to live with.

Not sure if this is the correct answer - I wanted to comment, and found that I don't have enough reputation points :)

PS; someone mentioned using the FailoverAppender, and I can see that there is an ErrorHandler in log4j 2, however I don't know how to make them do the job of OnlyOnceErrorHandler. I'd be happy to remove this answer if there's something better!