Implementation of Log4j


  1. It is a Tracing or Logging Tool used in Specially in Production Environment. It is used to find success messages, information, warnings, 
    errors in application while using it.
  2. By Default any Message/Error will be printed on Console, which is temporary location, it can show only few lines like last 100 lines.
  3. To see all problems from beginning to till date use Log4j concept.
  4. Log4J can write problems/error to File(.log), Database, Email, Network.etc..
  5. Log4J provides Error Details like Exception type, class and method with line number it occured, Date and time also other information ..
  6. Log4J also supports writing errors to Console also.
  7. Using System.out.println prints only on console, but not to file or any other memory.

Log4J has 3 components:

  1. Layout
  2. Appender
  3. Logger

1. Logger (LOG) Object: This object must be created inside the class as a instance variable. It is used to enable Log4J service to current class. If this object is not created in the class then Log4J concpet will not applicable(will not work)for that class

It has 5 priority methods with names (along with order)

Order Method Name
1 debug(obj) DEBUG
2 info(obj) INFO
3 warn(obj) WARN
4 error(obj) ERROR
5 fetal(obj) FETAL
  1. debug(msg) : It prints a message with data. It is used to print a final result of process. Like EmpId after saved is : 2362.
  2. info(msg) : It is used to print a simple message. Like process state-I done, if block end. Email sent etc..
  3. warn(msg): It is used to print warning messages. Like Collection is not with Generic, local variable not used, resource not closed etc..
  4. error(msg): It is used to print Exceptions like NullPointer, ArrayIndex, SQLException etc..
  5. fatal(mgs) : It indicates very high level problem. Like Server/DB Down, Connection timeout, Network broken, Class Not Found etc...
  6. OFF is used to disable Log4J concept in application. Log4J code need to be deleted. 

2. Appender : It will provide details for "Where to print Message?". Means in what type of memories, messages must be stored. To write Logger Statements to
i. File use FileAppender
ii. Database use JdbcAppender
iii. Email use SmtpAppender
iv. Console use ConsoleAppender
v. Network use Ftp(Telnet)Appender
In one Application we can use more than one appender also. 

3. Layout : It provide the format of message to be printed. Possible layouts are: 

  1. Simple Layout : Print message as it is
  2. HTML Layout : Print message in HTML format(....) 
  3. XML Layout: Print message in XML format 
  4. Pattern Layout : Prints messages in given pattern. example pattern: Date-Time / Line Number : Class-method :- Message

Example: Add below dependencies in pom.xml Files

Create one class (TestApp.Java) under "src/main/java" and write Log4J 

package in.nit;


import org.apache.log4j.Appender;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.HTMLLayout;
import org.apache.log4j.Layout;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.SimpleLayout;

public class TestApp {
    private static Logger log=Logger.getLogger(TestApp.class);
    public static void main(String[] args) throws IOException {
        //Layout layout=new SimpleLayout();
        //Layout layout=new HTMLLayout();
        //Layout layout=new PatternLayout("%p %d %C %M %m %n");
        //Appender app=new ConsoleAppender(layout);
        //Appender app=new FileAppender(layout,"c:/Log/data.log");
        log.debug("I am from Debug");"I am from Info");
        log.error("I am from Error");

#Root Logger

#Appender+Layout (ConsoleAppender)
log4j.appender.sout.layout.ConversionPattern=%p %d %C %M %m %n

#Appender+Layout (FileAppender)
log4j.appender.myfile.layout.ConversionPattern=%p %d %C %M %m %n

# Define the Jdbc appender
log4j.appender.db.sql=INSERT INTO LOGS VALUES ('%M', now(),'%C','%p','%m') 

#configuring the SMTP appender of messages %m %n