Translate

Monday, November 12, 2012

Log4j



Log4J is an Open Source logging framework from the apache foundation.  Log4j simply inserts a log statement in the application code and manage them externally without going back to its application source code. Programmer's can control the logging message with the help of external configuration file e.g. log4.xml or log4j.properties file.

The external properties file can be used to set the log format as well as the level of logging  (DEBUG, INFO, FATAL etc..)




 Log4j Architecture

The architecture of Log4J framework is layered and consists of three main components. There components of the Log4J are:

                            1) Logger
                             2) Appender
                             3) Layout
1. Logger:Logger is the most essential component of the logging process. It is responsible for capturing the logging information. There are 5 different log levels of the Logger.

DEBUG : Most useful to debug an application.
INFO : It provides informational messages.
WARN : It provides that application may have harmful events.
ERROR : It provides that application having error events but that might allow it to continue running.
FATAL : It denotes the severe error events which lead the application to abort.
 In addition, there are two special levels also they are:
ALL : It is intended to turn on all logging
OFF : It is intended to turn off logging

2. Appenders in Log4J:

The Appender is responsible for publishing the log to a destination. It controls how the logging provides the output. There are few list of appenders listed below:

ConsoleAppender
DailyRollingFileAppender
FileAppender
RollingFileAppender
WriterAppender
SMTPAppender
SocketAppender
SocketHubAppender
SyslogAppendersends
TelnetAppender

 ConsoleAppender is used as:  ConsoleAppender appender = new ConsoleAppender(new PatternLayout()); 

FileAppender is used as: FileAppender appender = new FileAppender(new PatternLayout(),"filename");

 WriterAppender is used as:appender = new WriterAppender(new PatternLayout(),new FileOutputStream("filename"));

3. Layouts in Log4J:

For each Appender it needs to have an associated Layout, which guides how to format the output. The Layout is responsible for formatting the log output in different layouts. User can control the output format by modifying the Log4J configuration file.
There are basically three types of Layout:

HTMLLayout : It formats the output in the HTML table
PatternLayout : It formats the output in the conversion pattern
SimpleLayout : It formats the output in a simple manner, it prints the level then place a dash and then the user specified log message.

How to Configuring Log4J

For running application using Log4J you need to download the latest version log4j jar file and then add this to the classpath.

There are two ways to configure the Log4J one is by using properties file and other by using xml file. Using xml for Log4J is quite popular and it is recommended also

Ex:-
import org.apache.log4j.Logger;

public class LogExample
{

   public LogExample() {  }
          static Logger log = Logger.getLogger(LogExample.class);

         public static void main(String argsp[])
         {
             log.debug("Here is some DEBUG");
             log.info("Here is some INFO");
             log.warn("Here is some WARN");
             log.error("Here is some ERROR");
             log.fatal("Here is some FATAL");
         }
}

Now how to configure Log4j .Properties file

log4j.rootLogger=debug, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%5p] %d{mm:ss}
(%F:%M:%L)%n%m%n%n


log4j logging file with the help of log4j.properties. Properties can also be defined within log4j.xml file. You have to add following contents to this XML file.

 
 

Note: if you are using both file’s first it looks xml file then it will go for properties file.


Console Appender in Log4j
Ex:-

import org.apache.log4j.*;
public class ConsoleAppenderExample
{
    static Logger logger = Logger.getLogger("ConsoleAppenderExample.class");
  public static void main(String[] args) 
{
        ConsoleAppender conappender =
             new ConsoleAppender(new PatternLayout());
         logger.debug("Content part 1");
         logger.debug("Content part 2");
         logger.debug("Content part 3");
        logger.addAppender(conappender);
  }
}

HTMLLayout in Log4j


import java.io.*;
import org.apache.log4j.*;
public class WriterAppenderExample 
{

          static Logger logger =
             Logger.getLogger("WriterAppenderExample.class");
         public static void main(String[] args)   
         {
          try
          {
               FileOutputStream filename=
                         new 
FileOutputStream("Writer.html");
               WriterAppender writeappender = 
                        new 
WriterAppender(new HTMLLayout(),filename);
               logger.addAppender(writeappender);
               logger.info("Welcome");
               logger.info("to");
               logger.info("Rose India");
               logger.info("Writer Appender");
               logger.info("Example");
               logger.info("-----------");
        }catch(Exception e)
        {
               System.out.println("Exception is ="+e.getMessage()); 
        } 
    }
}

Note: How to configure and run in myeclipse I am preparing video file. Here I will upload real time sample applications very soon.





No comments: