Javatpoint Logo
Javatpoint Logo

Log4j Appenders

Log4j provides Appender objects which are primarily responsible for printing logging messages to different destinations such as console, files, NT event logs, Swing components, JMS, remote UNIX syslog daemons, sockets, etc.

Each appender object has different properties related to it, and these properties specify the behavior of that object.

Property Description
layout Appender uses the Layout objects and the conversion pattern associated with them to format the logging information.
target The target may be a file, a console, or another item depending on the appender.
level It is necessary to control the filtration of the log messages.
threshold Appender can contain a threshold level associated with it independent of the logger level. The Appender ignores any logging messages that contain a level lower than the threshold level.
filter The Filter objects can read logging information beyond level matching and decide whether logging requests should be handled by a particular Appender or ignored.

We can add an Appender object to a Logger by adding the following setting in the configuration file with the given method.

We can also write same configuration in XML format as follows:

Even we can also add Appender object inside the program, and then we can use the following method:

The addAppender() method is used to add an Appender to the Logger object. We can add many Appender objects to a logger in a comma-separated list, each printing logging information to separate destinations.

We have a list of appenders. These are:

  • ConsoleAppender: Console Appender appends the log events to System.out or System.err using a layout specified by the user. System.out is a default target. It is useful for debugging purposes, but not much beneficial to use in a production environment.
  • FileAppender: Appends log events to a file. It supports two more appender classes:
  • RollingFileAppender, DailyRollingFileAppender: Both are the most widely used appenders that provide support to write logs to file.
  • JDBCAppender: The JDBCAppender is used 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 and executed.
  • SMTPAppender: It is used to send an email when a specific logging event occurs, typically on errors or fatal errors.
  • SocketAppender: It is used for remote storage.
  • SyslogAppender: It sends messages to a remote syslog domain.
  • TelnetAppender: It specializes in writing to a read-only socket.
  • WriterAppender: It is used to append log events to a Writer or an OutputStream depending on the user's choice.
  • AsyncAppender: The AsyncAppender allows users to log events asynchronously. The AsyncAppender will save the events sent to it and then dispatch them to all the appenders that are attached to it. We can attach multiple appenders to an AsyncAppender.
  • JMSAppender: A simple appender that publishes events to a JMS (Java Message Service) Topic. The events are serialized and transmitted as JMS message type ObjectMessage.

Others are:

  • AppenderSkeleton
  • LF5Appender
  • NTEventLogAppender
  • NullAppender
  • SocketHubAppender
  • ExternallyRolledFileAppender

Log4j Appender XML Configuration

Let's see the XML based configuration of commonly used ConsoleAppender and RollingFileAppender classes:

Log4j Appender Properties Configuration

Let's see a simple example showing appenders configuration through property file (log4j.properties). It is defining the above xml based configuration:


Next TopicLog4j vs SLF4J




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA