Log4j Appenders provides configuration for logging such as console, file, database etc. Below image shows the log4j Appender class hierarchy.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<!-- console appender --> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <param name="Target" value="System.out" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p %c{1} - %m%n" /> </layout> </appender> <!-- rolling file appender --> <appender name="file" class="org.apache.log4j.RollingFileAppender"> <param name="File" value="logs/main.log" /> <param name="Append" value="true" /> <param name="ImmediateFlush" value="true" /> <param name="MaxFileSize" value="10MB" /> <param name="MaxBackupIndex" value="5" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %d{Z} [%t] %-5p (%F:%L) - %m%n" /> </layout> </appender> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#Define console appender log4j.appender.console=org.apache.log4j.ConsoleAppender logrj.appender.console.Target=System.out log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%-5p %c{1} - %m%n #Define rolling file appender log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=logs/main.log log4j.appender.file.Append=true log4j.appender.file.ImmediateFlush=true log4j.appender.file.MaxFileSize=10MB log4j.appender.file.MaxBackupIndex=5 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n |
That’s all for a quick roundup on log4j appenders.
References: