Sunday, March 20, 2016

Log4j Tutorial

Log4j is an Open Source logging API developed under the Jakarta Apache project.It provides a roubst,reliable,fully configurable,easily extensible 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.


What is need for Logging?

Logging or writing the state of a program at various stages of its execution to some repository such as a log file, is an old method used for debugging and monitoring applications.

Inserting log statements manually is very tedious and time consuming,not to mention managing them(such ad modifying and updating)down the road due to various reasons such as ongoing upgrading and bug-fixing process for application code,and so forth.To ease this process, there is useful,efficient,and easy to use utility available called log4j API.


What is log4j?

Log4j is an open source logging API for java.This logging API currently in version 1.2.17. It 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.

What can log4j do?

1. Log4j handles inserting log statements in application code and managing them externally without touching application code,by using external configuration files.
2. Log4j categorizes log statements according to user-specified criteria and assigns different priority levels to these log statements.These priority levels decide which log statements are important enough to be logged to the log repository.
3.Log4j lets users choose from several destinations for log statements,such as console,file,databse,SMTP servers,GUI components etc.; with option of assigning different destinations to different categories of log statements.These log destinations can be changed anytime by simply changing log4j configuration files.
4.Log4j also facilitates creation of customized formats for log output and provides default formats in which log statements will be written to log destination.

How does log4j Work?

Logically, log4j can be viewed as being comprised of three main components:

Logger,appender and layout namely. The functionalities of each of these components are accessible through java classes of the same name.Users can extend these basic classes to create their own loggers,appenders and layouts.



Logger:

The component logger accepts log requests generated by log statements or printing methods during application execution and sends their output to appropriate destination,i.e. appenders specified by user.

The logger component is accessible through  the Logger class of the log4j API.This class provides a static method Logger.getLogger(name)that either retrieves and existing logger object by the given name,or creates a new logger of given name if none exists.This logger object is then used to set properties of logger component and invoke printing methods debug(),info(),warn(),error(),fatal() and log().

Log4j provides a default root logger that all user-defined loggers inherit from.Root logger is at the top of the logger hierarchy..


Priority levels of log statements:

logger can be assigned different levels of priorities. These priority levels decide which log statement is going to be logged.There are five different priority levels:
DEBUG,INFO,WARN,ERROR,and FATAL; in ascending order of priority.

Example of priority level of logger and log requests:


/*instantiate a logger named MyLogger*/

Logger mylogger=Logger.getLogger("MyLogger");

..........................................
..........................................

/*Set logger priority level to INFO programatically.Though this is better done externally*/

mylogger.setLevel(Level.INFO);

...................................
.................................

/*This log request is enabled and log statement logged,
Since INFO=INFO*/
mylogger.info("The value of paramenters passed to do_something() are:"+ a,b);

.........................................................
.........................................................

/* This log request is not enabled,since DEBUG<INFO */
mylogger.debug("operation performed successfully");

.................................................................
................................................................

/*THis log request is enabled and log statements logged,since ERROR>INFO */

mylogger.error("value of X is null");


APPENDER:

Appender component is interface to the destination of log statements. A logger object receives log request from log statements being executed,enables appropriate ones,and sends their output to the appender(s) assigned to it.There are various appenders available; such as ConsoleAppender(for console),FileAppender(for file),JDBCAppender(for database) etc..

An appender is assigned to a logger using the addAppender() method of the Logger class,or through external configuration files.

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 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 alo available in a log4j package; they are PatternLayout,DateLayout,HTMLLayout,and XMLLayout.

Implementing and Configuring log4j

implementing and configuring log4j is quite easy. The fallowing sections show  how to do it.

Requirements:

The only requirement for installing and using log4j is the source of log4j API,freely available for download fallow this link download log4j  in compressed(tar or zip)files.


Installation and running log4j:

After downloading the compressed log4j,uncompress it,save the resulting log4j-1.217.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.






No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...