Log4J Appenders
 


The ability to selectively enable or disable logging requests based on their logger is only part of the picture. Log4j allows logging requests to print to multiple destinations. In log4j speak, an output destination is called an appender. Currently, appenders exist for the console, files, GUI components, remote socket servers, JMS, NT Event Loggers, and remote UNIX Syslog daemons. It is also possible to log asynchronously. More than one appender can be attached to a logger.

The Appenders available are

Common Appenders

  FileAppender, it appends log events to a file.
  RollingFileAppender, it extends FileAppender to backup the log files when they reach a certain size.
  DailyRollingFileAppender -it extends FileAppender so that the underlying file is rolled over at a user chosen frequency.
  ConsoleAppender - it appends log events to System.out or System.err using a layout specified by the user. The default target is System.out.

Network Appenders

  SocketAppender - it sends LoggingEvent objects to a remote a log server, usually a SocketNode.
  SocketHubAppender - it sends LoggingEvent objects to a set of remote log servers, usually a SocketNodes .
  JMSAppender - A simple appender that publishes events to a JMS Topic. The events are serialized and transmitted as JMS message type ObjectMessage. .
  NTEventLogAppender - Append to the NT event log system.

Third-party Appenders

  JDBCAppender by Danko Mannhaupt (recommended over the current JDBCAppender included)
  SNMPTrapAppender

Special Appenders

  AsyncAppender - it lets users log events asynchronously. It uses a bounded buffer to store logging events.
  ExternallyRolledFileAppender - This appender listens on a socket on the port specified by the PORT_OPTION for a "RollOver" message. When such a message is received, the underlying log file is rolled over and an acknowledgment message is sent back to the process initiating the roll over.
  JDBCAppender - it provides for sending log events to a database.
  LF5Appender - it logs events to a swing based logging console. The swing console supports turning categories on and off, multiple detail level views, as well as full text searching and many other capabilties.
  NullAppender (there’s two, one for internal operations & one for benchmarking)
  SMTPAppender - it sends an e-mail when a specific logging event occurs, typically on errors or fatal errors.
  SyslogAppender - it sends messages to a remote syslog daemon.
  TelnetAppender - it is a log4j appender that specializes in writing to a read-only socket.
  WriterAppender - WriterAppender appends log events to a Writer or an OutputStream depending on the user's choice.

One may also implement the Appender interface to create ones own ways of outputting log statements.

Using A ConsoleAppender
A ConsoleAppender can be created like this:
ConsoleAppender appender = new ConsoleAppender(new PatternLayout());
Which creates a console appender, with a default PatternLayout. The default output of System.out is used.

Using A FileAppender
A FileAppender can be created like this:
FileAppender appender = null;
try {
appender = new FileAppender(new PatternLayout(),"filename");
} catch(Exception e) {}

The constructor in use above is:
FileAppender(Layout layout, String filename)
Instantiate a FileAppender and open the file designated by filename.

Another useful constructor is:
FileAppender(Layout layout, String filename, boolean append)
Instantiate a FileAppender and open the file designated by filename.

So that one may choose whether or not to append the file specified or not. If this is not specified, the default is to append.

Using A WriterAppender
A WriterAppender can be created like this:
WriterAppender appender = null;
try {
appender = new WriterAppender(new PatternLayout(),new FileOutputStream("filename"));
} catch(Exception e) {}

This WriterAppender uses the constructor that takes a PatternLayout and an OutputStream as arguments, in this case a FileOutputStream is used to output to a file, there are other constructors available.

 
 

Log4j
Log4j Features
Log4j Loggers
Log4j Appenders
Log4j Layouts
Log4j Levels
Log4j Installation
Log4j vs. JSR47
Pros & Cons of Log4j


Use of this website constitutes acceptance of the AllAppLabs.com Terms and Conditions