Saturday 17 October 2015

Log4j2 : AsyncAppender: Log asynchronously

AsyncAppender  references to other appenders and LogEvents are written to them on a separate Thread.

Following are the parameters for AsyncAppender.
Parameter
Type
Description
AppenderRef
String
Specifies the name of the Appenders to invoke asynchronously. You can configure multiple AppenderRef elements.
blocking
boolean
Default value is true. If true, the appender will wait until there are free slots in the queue. If false, the event will be written to the error appender if the queue is full.
bufferSize
integer
Default value is 128. Specifies the maximum number of events that can be queued.
errorRef
String
The name of the Appender to invoke if none of the appenders can be called, either due to errors in the appenders or because the queue is full. If not specified then errors will be ignored.
filter
Filter
Filters are used to filter the log events. Filters take the decision, whether to log the event (or) not. More than one Filter may be used by using a CompositeFilter.
name
String
The name of the appender.
ignoreExceptions
boolean
Default value is true, causing exceptions encountered while appending events to be internally logged and then ignored. When set to false exceptions will be propagated to the caller, instead. You must set this to false when wrapping this Appender in a FailoverAppender.
includeLocation
Boolean
Extracting location is an expensive operation (it can make logging 5 - 20 times slower). To improve performance, location is not included by default when adding a log event to the queue. You can change this by setting includeLocation="true".


log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
 <Appenders>
  <File name="my_file_appender" fileName="/Users/harikrishna_gurram/application.log">
   <PatternLayout>
    <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
   </PatternLayout>
  </File>
  <Async name="async_appender">
   <AppenderRef ref="my_file_appender" />
  </Async>
 </Appenders>
 <Loggers>
  <Root level="info">
   <AppenderRef ref="async_appender" />
  </Root>
 </Loggers>
</Configuration>

package log4j_tutorial;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class HelloWorld {
 private static final Logger logger = LogManager.getLogger();

 public static void main(String args[]) throws InterruptedException {
  logger.trace("Trace Message!");
  logger.debug("Debug Message!");
  logger.info("Info Message!");
  logger.warn("Warn Message!");
  logger.error("Error Message!");
  logger.fatal("Fatal Message!");
 }
}


Run HelloWorld application, you can able to see following messages in ‘application.log’ file.

$ cat application.log 
2015-10-05 09:55:24,730 INFO l.HelloWorld [main] Info Message!
2015-10-05 09:55:24,731 WARN l.HelloWorld [main] Warn Message!
2015-10-05 09:55:24,731 ERROR l.HelloWorld [main] Error Message!
2015-10-05 09:55:24,731 FATAL l.HelloWorld [main] Fatal Message!



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment