Log4j Appenders provides configuration for logging such as console, file, database etc. Below image shows the log4j Appender class hierarchy.

log4j-appenders-450x436

log4j Appender

This is the base of log4j Appenders that defines the methods to implement by an appender.

log4j AppenderSkeleton

This class provides the code for common functionality, such as support for threshold filtering and support for general filters. This is the base implementation that is extended by all other appenders such as JDBCAppender, AsyncAppender, ConsoleAppender etc. It has only one abstract method append(LoggingEvent event) that is required to be implemented by all appenders. We can write our own custom appender by implementing this method.

Commonly used log4j Appenders

Some of the most commonly used appenders are:

  • ConsoleAppender: ConsoleAppender appends log events to System.out or System.err using a layout specified by the user. The default target is System.out. It’s good for debugging purposes, but not much beneficial to use in production environment.
  • RollingFileAppender, DailyRollingFileAppender: These are the most widely used appenders that provide support to write logs to file. RollingFileAppender is used to limit the log file size and number of backup files to keep. DailyRollingFileAppender is used to log into files on date basis. However DailyRollingFileAppender has been observed to exhibit synchronization issues and data loss and not recommended to use.
  • JDBCAppender: The JDBCAppender provides for sending log events to a database. Each append call adds to an ArrayList buffer. When the buffer is filled each log event is placed in a sql statement (configurable) and executed. BufferSize, db URL, User, & Password are configurable options in the standard log4j ways.
  • AsyncAppender: 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. Note that we can configure it only through XML based i.e DOMConfigurator. This is useful when you are generating a lot of logs and don’t care if they are being logged instantly. There are chances of logs getting lost incase server crash. The AsyncAppender uses a separate thread to serve the events in its buffer.
  • JMSAppender: A simple appender that publishes events to a JMS Topic. The events are serialized and transmitted as JMS message type ObjectMessage.

Log4j Appenders XML Configuration

Below is the XML based configuration of commonly used ConsoleAppender and RollingFileAppender classes.

You can check the appender classes code to find out the parameters you can configure. For example in JDBCAppender you can configure databaseURL, databaseUser, databasePassword etc.

Log4j Appender Properties Configuration

A simple example showing appenders configuration through property file. It’s defining all the above xml based configuration.

That’s all for a quick roundup on log4j appenders.

References:

By admin

Leave a Reply

%d bloggers like this: