Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use AsyncAppender in log4j?

Tags:

log4j

appender

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?

like image 591
Sergey Avatar asked Jun 20 '12 03:06

Sergey


People also ask

What is async Appender in log4j?

The AsyncAppender lets users log events asynchronously. The AsyncAppender will collect the events sent to it and then dispatch them to all the appenders that are attached to it. You can attach multiple appenders to an AsyncAppender. The AsyncAppender uses a separate thread to serve the events in its buffer.

How do I use console Appenders in log4j2?

Log4j2 ConsoleAppender ConfigurationThe target poperty specifies the target of the logging messages i.e. SYSTEM_OUT or SYSTEM_ERR . The follow attribute tells whether the appender should honor the reassignments of System. out or System. err made after the logging configuration has been initialized.

What are Appenders in log4j?

Appenders. Apache log4j provides Appender objects which are primarily responsible for printing logging messages to different destinations such as consoles, files, sockets, NT event logs, etc. Each Appender object has different properties associated with it, and these properties indicate the behavior of that object.


1 Answers

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> 
like image 92
deepakmodak Avatar answered Sep 22 '22 09:09

deepakmodak