Saturday 21 June 2014

Log4j

Log4j (Logging For Java):


  • Keeping tracking of application flow of execution is called logging and the related messages that are generated are called log messages, there log messages are useful to analyse bugs to fix bugs that are generated in testing phase, MAINTENANCE PHASE of project.
  • Using System.out.println() to generate log messages System.out.println() is not recommended to use.
Draw Backs of System.out.println() are:

  1. Log messages written to console may disappear when screen scrollup.
  2. It is a single threaded process so can not write multiple log messages at a time.
  3. Does not allow to categorize log messages.
  4. Does not allow to Filter log messages while retrieving them.
  5. Can not write log message file, Database Software log messages and etc.
  6. Do not deliver the project to the client without log4j
  7. to overcome above problems we have to use 3rd party Logging API.
T he following Logging API'S

  1. Assertions (Sun Microsystems)
  2. Java Logging API (Sun M/s)
  3. Apache Commons Logging API (Apache)
  4. Log4j
  5. JBoss Logging( JBoss)
  6. SL4j (Simple Facade Logging for Java )(Apache)
Log4j Features:

  1. Allows to write log message to different destinations (like files, console, Database software etc..)
  2. Allows to format log messages
  3. Log4j is industry standard
  4. Allows to categorize log messages: Total 5 categories.
  5. Allows to filter log messages while retrieving them.

The 3 Important objects of Log4j:


  1. Logger object
  2. Appender object (Handler)
  3. Layout object (Formatter)
Logger Object:
  • Enables logging in the Application on per class basis
  • Important object to generate different categories of log messages:
syntax:

Logger logger=Logger.getLogger(com.rajendra.SelectTest.class);
logger.debug("....");
logger.info("..");
logger.warn("...");
logger.error("...");
logger.fatal("...);

  • Use Debug level for normal debug messages
  • Use "INFO" LEVEL when certain important operations are completed.
  • Use "WARN" level when deprecated api code, poor api, code is executed.
  • Use "ERROR" level in the catch block of Unchecked Exceptions
  • Use "FATAL" LEVEL in the catch blocks Checked Exceptions

.(2) Appender object:

  • Specify the destination to record log messages like file, console and etc.
  • Log4j api gives built-in Appenders.
Example: 

jdbc Appender
SMTC Appender
Console Appender
File Appender
Rolling File Appender
Daily Rolling File Appender and etc.

Rolling File Appender:

Creates new back up log files based on the max size of log file


Daily Rolling File Appender:




It creates 1 log file per day

(3) Layout object (Formatter)


  • Allows/Specifies the format and layout of log messages
  • Built-in layout classes.
Example:
  1.     HTML Layout
  2.     XML Layout
  3.     PATTERN Layout
  4.     Simple Layout
Putting all Together:

//enables logging
Logger logger=Logger.getLogger(com.ds.SelectTest.class); //here "ds" gives object of java.lang.class                                                                                                     //pointing to SelectTest
( Logger class is a Singleton java class)
//prepare layout.
SimpleLayout layout=new FileAppender("log.txt", true,layout);
//Add appender object to Logger object
logger.addAppender(appender);
//write log messages

logger.debug("....");
logger.info("..");
logger.warn("...");
logger.error("...");
logger.fatal("...);


  • to use Log4j api in our applications to generate log messages we need to add log4j<version>/jar file classpath or buildpath. You can download this jar file from www.logging.apache.org (or) from Weblogic installations.
  • Log4j support to a project is added during the development of project but the generated log messages are useful to fix the bugs that are generated during project testing and maintanance.


No comments:

Post a Comment