Thursday, December 25, 2014

Log4J


Log4j – Introduction
Log4j is Fast, Flexible, Reliable and easily configurable Logging framework which is open source API distributed under Apache Software License, written in Java.
Log4j has been ported to the C, C++, C#, Perl, Python, Ruby, and Eiffel languages.
Log4j is highly configurable through external configuration files at runtime. It views the logging process in terms of levels of priorities and offers mechanisms to direct logging information to a great variety of destinations, such as a database, file, console, UNIX Syslog etc.
Log4j is easily configurable by external xml or property file and we can configure log4j in java class itself and log4j can produce log message in following formats.

1. File
2.      HTML
3.      Database
4.      Console

Components of Log4j
There three main components available in Log4j
1.    Logger – Responsible for capturing logging information.
2.      appenders – Responsible for publishing logging information in different formats.
3.      layouts – Responsible to format logging information in different styles.

Features of Log4j
1.      log4j is thread-safe.
2.      log4j is optimized for speed.
3.      log4j supports multiple output appenders per logger.
4.      Logging behavior can be set at runtime using a configuration file.
5.      log4j supports internationalization.
6.      log4j is not restricted to a predefined set of facilities.
7.      log4j is designed to handle Java Exceptions from the start.
8.      log4j uses multiple levels, namely ALL, TRACE, DEBUG, INFO, WARN, ERROR and FATAL
9.      The target of the log output as well as the writing strategy can be altered by implementations of the Appender interface.
10.  log4j is fail-stop. However, altough it certainly strives to ensure delivery, log4j does not guarantee that each log statement will be delivered to its destination.

log4j Installation:
Log4j API package is distributed under the Apache Software License, a fully-fledged open source license certified by the open source initiative.
The latest log4j version, including full-source code, class files and documentation can be found at http://logging.apache.org/log4j/

log4j - Architecture
Log4j contains two types of objects.
1. Core Objects – These are mandatory required object to use log4j framework
2. Support Objects – These are optional objects which give support to core object.
1.      Core Objects: Core Objects are categorized in following three categories.

1.1  Logger : This is most important component of log4j.It is responsible for capture the logging information. Logger contains following levels.
1.1.1           Debug -  Useful for debug an application
1.1.2          Info - Provides informational messages
1.1.3           Warn - Provide the details of harmful events
1.1.4        Error – It provide the error and exception thrown in application and allow the application     continue its execution
1.1.5           Fatal - It provides the details severe error events like application may leads to abort
1.1.6          ALL - Used to turn on all logging level.
1.1.7          OFF - It is used to turn off all logging level

1.2  Appenders in log4 : The Appender is responsible to publish log messages in desired destination.Log4j provides following appenders.

1.2.1        Console Appender
1.2.2        DailyRollingFileAppender
1.2.3        FileAppender
1.2.4        RollingFileAppender
1.2.5        WriterAppender
1.2.6        SMTPAppender
1.2.7        WriterAppender
1.2.8        SocketAppender
1.2.9        SocketHubAppender
1.2.10    SyslogAppender
1.2.11    JDBCAppender
1.2.12    TelnetAppender
1.2.13    AppenderSkeleton
1.2.14    AsyncAppender
1.2.15    ExternallyRollingFileAppender
1.2.16    JMSAppender
1.2.17    LF5Appender
1.2.18    NTEventLogAppender
1.2.19    NullAppender

1.3  Layouts in log4j : Layout is responsible for how to format layout.Types of log4j layouts are given below
1.3.1        HTMLLayout – It format output in table formatted output in html file.
1.3.2        PatternLayout – It format the output in given conversion pattern with all supported appenders
1.3.3        SimpleLayout – It provide simple formatted output. It output will be log level followed by log message
1.3.4        XMLLayout – Its provide the xml formatted output

2.      Supported Objects :

2.1  Level Objects : The level objects defines the priority to logging. Here I listed the levels in their priority

2.1.1        ALL
2.1.2        TRACE
2.1.3        DEBUG
2.1.4        INFO
2.1.5        WARN
2.1.6        ERROR
2.1.7        FATAL
2.1.8        OFF

2.2  FILTER: Filter is object applied on logger and make further decision on whether information should be logged or not.

2.3  ObjectsRenderer : The object renderer objects provides the string representation of different objects passed to log4j framework. This object used by layout to prepare final logging information.

2.4  LogManager : The LogManager is responsible for reading configuration information from configuration file or class.