Log 4 J
Log 4 J
Log 4 J
It's speed and flexibility allows log statements to remain in shipped code while
giving the user the ability to enable logging at runtime without modifying any of
the application binary. All of this while not incurring a high performance cost.
Logging does have its drawbacks. It can slow down an application. If too
verbose, it can cause scrolling blindness. To alleviate these concerns, log4j is
designed to be reliable, fast and extensible. Since logging is rarely the main
focus of an application, the log4j API strives to be simple to understand and to
use.
All enterprise architectures adopt a global logging framework. The logging
framework provides classes/interfaces for logging messages, errors and debug
statements to destinations like files, databases, e-mails and consoles throughout
the system. The logged information is useful to an end-user of an application or
to a system administrator. Internationalization in a logging framework is the ability
to log the messages in different languages. All logging frameworks will be rated
on internationalization and extensibility. The following logging frameworks are
available:
• Java TM Logging
• Jlog
• Log4j
• Protomatter
• BEA mechanism for logging (technically not a full feature framework)
• Message LFW
Logging with log4j—An Efficient Way to Log Java Applications
Log4j is an Open Source logging API developed under the Jakarta Apache
project. It provides a robust, reliable, fully configurable, easily extendible, and
easy to implement framework for logging Java applications for debugging and
monitoring purposes. Log4j allows developers to insert log statements in their
code and configure them externally. This article covers the need for logging; a
brief introduction to log4j; an explanation of its components and terminology,
implementation, and configuration; its advantages and shortcomings; and how to
use it to log Java applications.
What Is log4j?
Log4j is an Open Source logging API for Java. This logging API, currently in
version 1.2.8, became so popular that it has been ported to other languages such
as C, C++, Python, and even C# to provide logging framework for these
languages.
Logger
Appender
Layout
The Layout component defines the format in which the log statements are written
to the log destination by appender. Layout is used to specify the style and
content of the log output to be recorded; such as inclusion/exclusion of date and
time of log output, priority level, info about the logger, line numbers of application
code from where log output originated, and so forth. This is accomplished by
assigning a layout to the appender concerned.
Layout is an abstract class in log4j API; it can be extended to create user-defined
layouts. Some readymade layouts are also available in a log4j package; they are
PatternLayout, SimpleLayout, DateLayout, HTMLLayout, and XMLLayout.
Implementing and Configuring log4j
Implementing and configuring log4j is quite easy. The following sections show
how to do it.
Requirement
The only requirement for installing and using log4j is the source of log4j API,
freely available for download in compressed (tar or zip) files (see Resources).
Installation and running log4j
Download the compressed log4j source, uncompress it, save the resulting log4j-
1.2.4.jar at any desired location and include its absolute path in the application's
CLASSPATH. Now, log4j API is accessible to user's application classes and can
be used for logging.
To log an application class, follow these steps:
Import log4j package in the class.
Inside the class, instantiate a logger object using Logger.getLogger( ) static
method.
Instantiate layouts (readymade or user-defined) to be assigned to appenders.
Instantiate appenders and assign desired layout to them by passing the layout
object as parameter to their constructors.
Assign the instatiated appenders to the Logger object by invoking its
addAppender( ) method with desired appender as parameter.
Invoke appropriate printing methods on Logger object to perform logging.
Steps 3, 4, and 5 can be skipped in case of external configuration.
Conclusion
The log4j API provides a means for inserting useful and manageable log
statements in the application code, assigning priority levels to them to make sure
only relevant log statements get logged, and specifying formats the log
statements are written in and managing log statements externally with no need of
changing containing application's code, recompile, or redeployment.