February 06, 2011

Log4J

An Idea of Log4J:
----------------

what is Log4J and it's advantages & disadvantages ...
Log4j API is an Apache project. It provides you the logging mechanism. which allows you to direct the debug & error (basically) statments onto a particular file/database/messaging system etc. Its provide a much cleaner way to store the output statment during the program execution. It can record information upto a class level. For ex: You have X Y Z ...N classes....lets say Z classe has thrown and exception/error which you logged using Log4j while finding the rootcause you would see those error/debug files...It will show you the absolute class name as well if configured so. Log4j is fully configurable using either. properties or .xml.
Disadvantage is it take 5 nanoseconds each time to log a statement or when it is invoked.

Introduction of LOG4J:
---------------------

Logging within the context of program development constitutes inserting statements into the program that provide some kind of output information that is useful to the developer. Examples of logging are trace statements, dumping of structures and the familiar System.out.println or printf debug statements. log4j offers a hierarchical way to insert logging statements within a Java program. Multiple output formats and multiple levels of logging information are available.

By using a dedicated logging package, the overhead of maintaining thousands of System.out.println statements is alleviated as the logging may be controlled at runtime from configuration scripts. log4j maintains the log statements in the shipped code. By formalising the process of logging, some feel that one is encouraged to use logging more and with higher degree of usefulness.

How to Instal:
-------------

In order to use the tools we are about to install it is necessary to setup the operating environment so that the tools know where to find stuff they need and the operating system knows where to find the tools. A understanding of how to do this is essential as you will be asked to change the operating environment. I have comprehensively covered this in documents entitled Configuring A Windows Working Environment and Configuring A Unix Working Environment.
Download the log4j distribution from http://jakarta.apache.org/log4j/docs/download.html.

Extract the archived files to some suitable directory.

Add the file dist/lib/log4j-1.2.6.jar to your CLASSPATH environment variable.

Download http://apache.rmplc.co.uk/dist/xml/xerces-j/Xerces-J-bin.2.6.0.zip and unzip it to a temporary directory. Copy the files xercesImpl.jar and xmlParserAPIs.jar to some permanent location and append their paths to the CLASSPATH environment variable.

log4j Basic Concepts:

The use of log4j revolves around 3 main things:

public class Logger

Logger is responsible for handling the majority of log operations.

public interface Appender

Appender is responsible for controlling the output of log operations.

public abstract class Layout

Layout is responsible for formatting the output for Appender.

Logger:
------

The logger is the core component of the logging process. In log4j, there are 5 normal levels Levels of logger available (not including custom Levels), the following is borrowed from the log4j API (http://jakarta.apache.org/log4j/docs/api/index.html):

static Level DEBUG

The DEBUG Level designates fine-grained informational events that are most useful to debug an application.

static Level INFO

The INFO level designates informational messages that highlight the progress of the application at coarse-grained level.

static Level WARN

The WARN level designates potentially harmful situations.

static Level ERROR

The ERROR level designates error events that might still allow the application to continue running.

static Level FATAL

The FATAL level designates very severe error events that will presumably lead the application to abort.

In addition, there are two special levels of logging available: (descriptions borrowed from the log4j API http://jakarta.apache.org/log4j/docs/api/index.html):

static Level ALL

The ALL Level has the lowest possible rank and is intended to turn on all logging.

static Level OFF

The OFF Level has the highest possible rank and is intended to turn off logging.

The behaviour of loggers is hierarchical. The following table illustrates this:

Figure?1.?Logger Output Hierarchy
Logger Output Hierarchy

A logger will only output messages that are of a level greater than or equal to it. If the level of a logger is not set it will inherit the level of the closest ancestor. So if a logger is created in the package com.foo.bar and no level is set for it, it will inherit the level of the logger created in com.foo. If no logger was created in com.foo, the logger created in com.foo.bar will inherit the level of the root logger, the root logger is always instantiated and available, the root logger is assigned the level DEBUG.

There are a number of ways to create a logger, one can retrieve the root logger:

Logger logger = Logger.getRootLogger();

One can create a new logger:

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

More usually, one instantiates a static logger globally, based on the name of the class:

static Logger logger = Logger.getLogger(test.class);

All these create a logger called "logger", one can set the level with:

logger.setLevel((Level)Level.WARN);

You can use any of 7 levels; Level.DEBUG, Level.INFO, Level.WARN, Level.ERROR, Level.FATAL, Level.ALL and Level.OFF.

Appender:
--------

The Appender controls how the logging is output. The Appenders available are (descriptions borrowed from the log4j API http://jakarta.apache.org/log4j/docs/api/index.html):

ConsoleAppender: appends log events to System.out or System.err using a layout specified by the user. The default target is System.out.

DailyRollingFileAppender extends FileAppender so that the underlying file is rolled over at a user chosen frequency.

FileAppender appends log events to a file.

RollingFileAppender extends FileAppender to backup the log files when they reach a certain size.

WriterAppender appends log events to a Writer or an OutputStream depending on the user's choice.

SMTPAppender sends an e-mail when a specific logging event occurs, typically on errors or fatal errors.

SocketAppender sends LoggingEvent objects to a remote a log server, usually a SocketNode.

SocketHubAppender sends LoggingEvent objects to a set of remote log servers, usually a SocketNodes

SyslogAppendersends messages to a remote syslog daemon.

TelnetAppender is a log4j appender that specializes in writing to a read-only socket.

One may also implement the Appender interface to create ones own ways of outputting log statements.
3.2.1.?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.
3.2.2.?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.
3.2.3.?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) {}

Layout:
------

The Appender must have have an associated Layout so it knows how to format the output. There are three types of Layout available:

HTMLLayout formats the output as a HTML table.

PatternLayout formats the output based on a conversion pattern specified, or if none is specified, the default conversion pattern.

SimpleLayout formats the output in a very simple manner, it prints the Level, then a dash '-' and then the log message.

3.4.?Basic Examples Illustrating this
3.4.1.?SimpleLayout and FileAppender

Here is a very simplistic example of a program implementing a SimpleLayout and FileAppender:

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
import org.apache.log4j.FileAppender;
public class simpandfile {
static Logger logger = Logger.getLogger(simpandfile.class);
public static void main(String args[]) {
SimpleLayout layout = new SimpleLayout();

FileAppender appender = null;
try {
appender = new FileAppender(layout,"output1.txt",false);
} catch(Exception e) {}

logger.addAppender(appender);
logger.setLevel((Level) Level.DEBUG);

logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");
}
}


You can download it: simpandfile.java. And checkout the output produced: output1.txt.
3.4.2.?HTMLLayout and WriterAppender

Here is a very simplistic example of a program implementing a HTMLLayout and WriterAppender:

import java.io.*;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.HTMLLayout;
import org.apache.log4j.WriterAppender;
public class htmlandwrite {
static Logger logger = Logger.getLogger(htmlandwrite.class);
public static void main(String args[]) {
HTMLLayout layout = new HTMLLayout();

WriterAppender appender = null;
try {
FileOutputStream output = new FileOutputStream("output2.html");
appender = new WriterAppender(layout,output);
} catch(Exception e) {}

logger.addAppender(appender);
logger.setLevel((Level) Level.DEBUG);

logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");
}
}


You can download it: htmlandwrite.java. And checkout the output produced: output2.html.
3.4.3.?PatternLayout and ConsoleAppender

Here is a very simplistic example of a program implementing a PatternLayout and ConsoleAppender:

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.ConsoleAppender;
public class consandpatt {
static Logger logger = Logger.getLogger(consandpatt.class);
public static void main(String args[]) {

// Note, %n is newline
String pattern = "Milliseconds since program start: %r %n";
pattern += "Classname of caller: %C %n";
pattern += "Date in ISO8601 format: %d{ISO8601} %n";
pattern += "Location of log event: %l %n";
pattern += "Message: %m %n %n";

PatternLayout layout = new PatternLayout(pattern);
ConsoleAppender appender = new ConsoleAppender(layout);

logger.addAppender(appender);
logger.setLevel((Level) Level.DEBUG);

logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");
}
}

Using External Configuration Files:
----------------------------------

Log4j is usually used in conjunction with external configuration files so that options do not have to be hard-coded within the software. The advantage of using an external configuration file is that changes can be made to the options without having to recompile the software. A disadvantage could be, that due to the io instructions used, it is slightly slower.

There are two ways in which one can specify the external configuration file: a plain text file or an XML file. Since everything is written in XML these days, this tutorial will focus on the XML approach but will also include relevant plain text examples. To begin with, examine the sample XML config file shown below:

The file starts with a standard XML declaration followed by a DOCTYPE declaration which indicates the DTD(Document Type Definition), this defines the structure of the XML file, what elements may be nested within other elements etc. This file is provided in the log4j distribution under src/java/org/apache/log4j/xml. Next comes the all-encapsulating log4j:configuration element, which was specified as the root element in the DOCTYPE declaration. Nested within the root element are two structures:


Here an Appender is created and called "ConsoleAppender", note that any name could have been chosen, it is because of the contrivity of examples that this name was chosen. The class for the appender is then specified in full, when referring to classes, one always uses the fully qualified class name. An Appender must always have a name and a class specified. Nested within Appender is the layout element which defines the layout to be a SimpleLayout. Layout must always have the class attribute.


The root element always exists and cannot be sub-classed. The example shows the priority being set to "debug" and the appender setup by including an appender-ref element, of which, more that one may be specified. See the file src/java/org/apache/log4j/xml/log4j.dtd in your log4j distribution for more information about the structure of an XML configuration file. The configuration file is pulled into the Java program like this:

DOMConfigurator.configure("configurationfile.xml");

The DOMConfigurator is used to initialise the log4j environment using a DOM tree. Here is the example xml configuration file: plainlog4jconfig.xml. Here is a program which implements this configuration file: files/externalxmltest.java:

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
public class externalxmltest {
static Logger logger = Logger.getLogger(externalxmltest.class);
public static void main(String args[]) {
DOMConfigurator.configure("xmllog4jconfig.xml");
logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");
}
}

Here is an XML configuration file for a Logger implementing a FileAppender using a PatternLayout:


You can download this example from here: xmllog4jconfig2.xml. For more examples of using xml files to configure a log4j environment, see the src/java/org/apache/log4j/xml/examples/ directory in the log4j distribution.

Here is the configuration file discussed above, expressed in the form of a plain text file:

# initialise root logger with level DEBUG and call it BLAH
log4j.rootLogger=DEBUG, BLAH
# add a ConsoleAppender to the logger BLAH
log4j.appender.BLAH=org.apache.log4j.ConsoleAppender
# set set that layout to be SimpleLayout
log4j.appender.BLAH.layout=org.apache.log4j.SimpleLayout


You can download it here: plainlog4jconfig.txt. Here is a program implementing this:

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class externalplaintest {
static Logger logger = Logger.getLogger(externalplaintest.class);
public static void main(String args[]) {
PropertyConfigurator.configure("plainlog4jconfig.xml");
logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");
}
}

You can download an example program that uses this configuration file here: files/externalplaintest.java. For more examples of using plain text files to configure a log4j environment, see the examples directory in the log4j distribution.

The use of external example files has only been briefly discussed here, it is assumed that you have the capacity to learn more by yourself by studying the examples provided with the log4j distribution and experimenting.

-----------------------------------------------------------------------------
Apache Log4j:

Logging Framework is an abstraction that hide details related to how logging
to be done, from end user. As Log4j is configuration based logging framework,
one can define how logging has to be done, by defining appropriate configuration
for File/JDBC or both

What would be file name or JDBC related connection URL with credentials

Levels of root logger, such as DEBUG, WARN, INFO, ERROR, FATAL etc

Many more, those as covered in example and articles below:

http://www.interview-questions-tips-forum.net/index.php/Apache-Log4j

-----------------------------------------------------------------------------

Real time :[Using Log4j Example : A case study discussed]

DISCLAIMER :
The content provided in this page is not warranted or guaranteed by Interview-Questions-Tips-Forum.net.
We are not liable for any negative consequences that may result/arise from implementing
directly/indirectly any information coveredin these pages/articles/tutorials and these content
provided in this web site are intented for educational purpose only.

Please be informed that NONE of the design/code/matter from this page is claiming to be some
sort of best practice and we DO NOT expect any of our visitor/reader of this page to assume
this as some sort of best practice for any context and should not be using this as it is
without appropriate evaluation.
This page intends only to provide bits and pieces of known ways for doing some sort of example
and may not be fit for any other purpose.

In spite of all precautions taken to provide accurate and avoid any typo in these pages,
there might be some issues like grammatical mistakes and typo being observed in these pages,
We extend our sincerest apologies for the same.


Log4j Example : Using various levels of logging using Apache Log4j

Suppose there is a log file to be analyzed for certain issues while
tracing activity is performed. This activity can be real difficult
if one has to find/look for a pin from a box full of sand, type of
situation. This may happen if number of tracing / logging lines of
statements ranges from few hundred to thousand lines for a real
big application with many classes being executed with an operation.

So what do you think can be possible way to simplify this particular
problem?? In my thinking if there could be some way to filter only
those log statements, those are related to one particular class
or component in hand.

Yes, You are right!!! Apache Log4j has a provision for defining
which class to be considered for logging and leaving rest of the
classes not eligible for logging purpose.

In this example I shall try to demonstrate this objective of
configuring log4j.properties file in such a way so as to provide
a filter kind of functionality and this should be done without
any code change as far as the Java class files are concerned.
So this is pretty configuration time setting up activity.

In order to show a method execution flow, I have defined three dummy
class files, such as ExampleController, ExampleService, ExampleDAO
with some dummy kind of methods too.

demo.example.ExampleController.java

package demo.example;

import org.apache.log4j.Logger;

public class ExampleController {
private static Logger logger = Logger.getLogger(ExampleController.class);
public void delegateCall() {
logger.info("Entering delegateMethod");
new ExampleService().doBusiness();
logger.info("Leaving delegateMethod");
}
}
demo.example.ExampleService.java

package demo.example;

import org.apache.log4j.Logger;

public class ExampleService {
private static Logger logger = Logger.getLogger(ExampleService.class);
public void doBusiness() {
logger.info("Entering doBusiness");
new ExampleDAO().doStore();
logger.info("Leaving doBusiness");
}
}

demo.example.ExampleDAO.java

package demo.example;

import org.apache.log4j.Logger;

public class ExampleDAO {
private static Logger logger = Logger.getLogger(ExampleDAO.class);
public void doStore() {
logger.info("Entering doStore");
logger.debug("TODO database call");
logger.info("Leaving doStore");
}
}

As these files shows that ExampleController.delegateCall method
is calling ExampleService.doBusiness and this method is again
calling ExampleDAO.doStore, so there is a chain of method
execution with some logging statements being coded.
Now a very common (I think so) log4j properties file for this example
as follows:
log4j.rootLogger=INFO, A
log4j.appender.A=org.apache.log4j.ConsoleAppender
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%d [%t] %-5p %C - %m%n
This log4j.properties file prints out all the logging statements
that Log4j encounters while these method execution, as shown below:
[main] INFO demo.example.ExampleController - Entering delegateMethod
[main] INFO demo.example.ExampleService - Entering doBusiness
[main] INFO demo.example.ExampleDAO - Entering doStore
[main] INFO demo.example.ExampleDAO - Leaving doStore
[main] INFO demo.example.ExampleService - Leaving doBusiness
[main] INFO demo.example.ExampleController - Leaving delegateMethod
Now I have modified log4j.properties file and added the line marked
as bold RED in color
log4j.rootLogger=FATAL
log4j.logger.demo.example=INFO, A
log4j.appender.A=org.apache.log4j.ConsoleAppender
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%d [%t] %-5p %C - %m%n
This makes no difference so far as the number of logging statements
on console, for this example is concerned :
But the real difference appeared when I added the class file name
within the second line of log4j properties file as follows:
log4j.rootLogger=FATAL
log4j.logger.demo.example.ExampleController=INFO, A
log4j.appender.A=org.apache.log4j.ConsoleAppender
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%d [%t] %-5p %C - %m%n
This makes the log statement being printed on console, restricted
only to the demo.example.ExampleController class file, as show below:
[main] INFO demo.example.ExampleController - Entering delegateMethod
[main] INFO demo.example.ExampleController - Leaving delegateMethod
By this way one can apply some filter/restrictions (Just of understanding)
and log only those statements those are real important to track.

No comments:

Post a Comment

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts