Rahman created LOG4J2-2772:
------------------------------

             Summary: KafkaAppender when used with FailoverAppender does not 
work
                 Key: LOG4J2-2772
                 URL: https://issues.apache.org/jira/browse/LOG4J2-2772
             Project: Log4j 2
          Issue Type: Bug
          Components: Appenders
    Affects Versions: 2.12.1
            Reporter: Rahman


I applied a KafkaAppender to the FailoverAppender and configured a 
RollingFileAppender as one of the failoverAppenders. I used log4j 2.12.0 
version. I deliberately switched off the kafka server and found that it does 
not switch to the secondary RollingFileAppender. Below is the snapshot of the 
configuration used.
{code:java}
// code placeholder

<Failover name="LOG_FAILOVER" primary="LOG_KAFKA">
 <Failovers>
 <AppenderRef ref="LOG_KAFKA_FAILOVER_APPENDER"/>
 </Failovers>
 </Failover>
<Kafka name="LOG_KAFKA" topic="${log.kafka.message.batch.cache.topic}" 
ignoreExceptions="false">
 <JsonLayout compact="false" eventEol="true" properties="true"/>
 <Property name="bootstrap.servers" value="${log.kafka.bootstrap.servers}"/>
 <Property name="zookeeper.connect" value="${log.kafka.zookeeper.connect}"/>
 </KafkaFix>
<RollingFile name="LOG_KAFKA_FAILOVER_APPENDER" 
fileName="${log.directory}/MessageBatchCacheKafkaFailOver.log" 
filePattern="${log.directory}/MessageBatchCacheKafkaFailOver.log.%d{yyyy-MM-dd}.gz">
 <JsonLayout compact="false" eventEol="true" properties="true"/>
 <Policies>
 <TimeBasedTriggeringPolicy interval="1" modulate="true" />
 </Policies>
 </RollingFile>
{code}
 

 

Upon looking for issues I found a fix for 
https://issues.apache.org/jira/browse/LOG4J2-1103. But this issue did not 
resolve the problem. Upon looking at the source code I modified the following 
append method

 
{code:java}
// code placeholder

@Override
    public void append(final LogEvent event) {
        if (event.getLoggerName() != null && 
event.getLoggerName().startsWith("org.apache.kafka")) {
            LOGGER.warn("Recursive logging from [{}] for appender [{}].", 
event.getLoggerName(), getName());
        } else {
            try {
                tryAppend(event);
            } catch (final Exception e) {
                if (ignoreExceptions()){
                    error("Unable to write to Kafka in appender [" + getName() 
+ "]", event, e);
                } else {
                    throw new AppenderLoggingException("Unable to write to 
Kafka in appender [" + getName() + "]", e);
                }
            }
        }
    }
{code}
with the additions of the following lines in the catch statement.
{code:java}
// code placeholder

if (ignoreExceptions()){ error("Unable to write to Kafka in appender [" + 
getName() + "]", event, e); } else { throw new AppenderLoggingException("Unable 
to write to Kafka in appender [" + getName() + "]", e); }
{code}
As the FailoverAppender requires AppenderLoggingException for it to switch to 
other secondary Appender, I found that in this method we were simply logging 
the exception.

 

Kindly review the bug and let us know if there is an alternative solution than 
the above.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to