Saturday 17 October 2015

Log4j2: FailoverAppender

By using FailoverAppender, you can achieve failover mechanism, The FailoverAppender wraps a set of appenders, if the primary appender failed, then the secondary will be tried and so on.

Following table shows parameters of FailoverAppender.
Parameter
Type
Description
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.
primary
String
Name of the primary appender to use.
failovers
String[]
Names of the secondary appenders to use.
name
String
Name of the appender
retryIntervalSeconds
integer
The number of seconds that should pass before retrying the primary Appender. The default is 60 seconds.
ignoreExceptions
boolean
If ignoreExceptions set to false exceptions will be propagated to the caller. If ignoreExceptions is set to true, exceptions encountered while appending events to be internally logged and then ignored.
Default value is true.
target
String
Either "SYSTEM_OUT" or "SYSTEM_ERR". Default value is SYSTEM_ERR.


log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG" monitorInterval="30">
 <Appenders>
  <Console name="my_console_appender" target="SYSTEM_OUT">
   <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
  </Console>
  <File name="my_file_appender" fileName="/Users/harikrishna_gurram/application.log">
   <PatternLayout>
    <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
   </PatternLayout>
  </File>

  <Failover name="failover_appender" primary="my_file_appender">
   <Failovers>
    <AppenderRef ref="my_console_appender" />
   </Failovers>
  </Failover>
 </Appenders>
 <Loggers>
  <Root level="info">
   <AppenderRef ref="failover_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!");
  Thread.sleep(20000);
  logger.debug("Debug Message!");
  logger.info("Info Message!");
  logger.warn("Warn Message!");
  logger.error("Error Message!");
  logger.fatal("Fatal Message!");
 }
}



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment