How to use AsyncAppender in log4j?

Sergey picture Sergey · Jun 20, 2012 · Viewed 45.7k times · Source

How to use AsyncAppender in log4j in order to write log message to the web service? Should I create my own Appender which would extend AsyncAppender or just attach custom appenders to the AsyncAppender? If the second choice is correct, where should I take the AsyncAppender object? Is there any example?

Answer

deepakmodak picture deepakmodak · Oct 23, 2012

Add AsyncAppender in log4j config file which will refer to a real appender. For demo: adding asyncappender to console appender in log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="[%p] %d{dd MMM hh:mm:ss aa} %t [%l] %m%n"/>
    </layout>
</appender>
<appender name="async" class="org.apache.log4j.AsyncAppender">
    <param name="BufferSize" value="500"/>
    <appender-ref ref="console"/>
</appender>
<root>
    <priority value="all"></priority>
    <appender-ref ref="async"/>
</root>
</log4j:configuration>