Wednesday 23 November 2016

Log4j

What Is Log4j:
While developing Java/J2EE applications, for debugging an application that is to know the status of a java application at its execution time, in general we use system.out.println statements in the application right…
But we have some disadvantages while using SOPL (system.out.println) statements.
  • Generally SOPL statements are printed on console, so there are temporary messages and when ever the console is closed then automatically the messages are removed from the console
  • It is not possible to store the SOPL messages in a permanent place and these are single threaded model, means these will prints only one by one message on the console screen
  • In order to overcome the problems  of  SOPL statements Log4j came into picture, with Log4j we can store the flow details of  ourJava/J2EE in a file or databases
  • This is a Open Source tool given by Apache, for only java projects, to record or write the status of an application at various places
  • Working with log4j is nothing but working with classes & interfaces given in org.apache.log4j.*
  • Log4j is a common tool, used for small to large scale Java/J2EE projects
  • In Log4j we use log statements rather SOPL statements in the code to know the status of a project while it is executing
  • In real time, after a project is released and it is installed in a client location then we call the location as on-site right, when executing the program at on-site location, if we got any problems occurred then these problems must report to the off showered engineers,  in this time we used to mail these Log files only so that they can check the problems easily

Components of Log4j:
  1. Logger
  2. Appender
  3. Layout

Logger

  • Logger is a class, in org.apache.log4j.*
  • We need to create Logger object one per java class
  • This component enables Log4j in our java class
  • Logger methods are used to generate log statements in a java class instead of sopls
  • So in order to get an object of Logger class, we need to call a static factory method [ factory method will gives an object as return type ]
  • We must create Logger object right after our class name, i will show you 

Getting Logger Object

static Logger log = Logger.getLogger(YourClassName.class.getName())
Note:  while creating a Logger object we need to pass either fully qualified class name or class object as a parameter, class means current class for which we are going to use Log4j.

Logger object has some methods, actually we used to print the status of our application by using these methods only
We have totally 5 methods in Logger class
  • debug()
  • info()
  • warn()
  • error()
  • fatal()

Appender

Appender job is to write the messages into the external file or database or smtp
  • Logger classes generates some statements under different levels right, this Appender takes these log statements and stores in some files or database
  • Appender is an interface
In log4j we have different Appender  implementation classes
  • FileAppender [ writing into a file ]
  • ConsoleAppender [ Writing into console ]
  • JDBCAppender [ For Databases ]
  • SMTPAppender [ Mails ]
  • SocketAppender [ For remote storage ]
  • SocketHubAppender
  • SyslogAppendersends
  • TelnetAppender
Again in FileAppender we have 2 more
  • RollingFileAppender
  • DailyRollingFileAppender
For now just remember, i will explain while executing the program

Layout

This component specifies the format in which the log statements are written into the destination repository by the appender
We have different type of layout classes in log4j
  • SimpleLayout
  • PatternLayout
  • HTMLLayout
  • XMLLayout

Example Program:

import org.apache.log4j.Appender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.HTMLLayout;
import org.apache.log4j.Layout;
import org.apache.log4j.Logger;
 
public class Client 
{
 
  static Logger l = Logger.getLogger(Client.class.getName());
 
  public static void main(String[] args) {  
 
 Layout l1 = new HTMLLayout();
 Appender a;
 //Appender a = new ConsoleAppender(l1);
 try
 {
 a = new FileAppender(l1,"my.txt", false);
 
 // 3rd parameter is true by default
 // true = Appends the data into my.txt
 // false = delete previous data and re-write
 
 l.addAppender(a);
 }
 catch(Exception e) {}  
 
 l.fatal("This is the error message..");
 System.out.println("Your logic executed successfully....");
  }
}




No comments:

Post a Comment