How to use Log4j

Posted by Unknown | 10:15

Welcome guys, This is my first post in my blog "abdobean"
This post will bring you at  acceptable level to understand the basic concept of log4j. let's break the ice between you and Log4j framework.

Firstly what is the Log4j?
 Log4j is a Reliable, Fast and Flexible Logging Framework (APIs) written in Java which is distributed under  the Apache Software License.
To use Log4j you need to know three component that consider the main component in log4j framework
  1. loggers : responsible for capturing or getting logging information
  2. appenders :  responsible for displaying the logging information in you preferred destination
  3. layouts : responsible for formatting the logging information in different style such as(Date Layout,HTML Layout,XML Layout,Pattern Layout,simple Layout )
why use log4j framework?
From my perspective , Logging is an important component of the software development for many purposes, we need to know all interaction with system that helps developer to know where is problem when user found a strange behavior for system. logging is an important for software administration where logging provides for software administration to know who is person that interact with system in specific period if some problem happened  in this period.Logging has many advantages but why i recommend to use Log4j for logging? because the following reasons
  1. Logging behavior can change at run time using configuration file
  2. It is fully configurable at runtime using external configuration files
  3. log4j supports internationalization
  4. log4j provides many destination you can log in files, database,console,....
  5. log4j is optimized for speed
  6. log4j has many levels (Trace,Info,Debug,Error, Fatal,All,OFF,Warn),you can make your custom level(I will discuss level in details in next post )
Note "logging operation can  slow down an application"
Now let's start the practice part,this part will show how to use log4j to log in file
you need the following software and resources. 
  • netbeans
  •  log4j Here 
the steps
  1. Create new java desktop application in netbeans.
  2. Choose File > New Project  from the main menu. Select Java Application from the Java  category and click Next.
  3. Type log4jexample for the project name and set the project location.
  4. Add log4j-1.2.17.jar in application libraries
  5.  create log4j.properties file in default package 

  6. in this file i assigned the file appender to rootlogger and set the level of root logger with debug level, Appender will create folder log in user home direction and create log.txt file where log message in it. the message will save in format (the message and new line after it)
  7.  In Log4jeample.java create static object from logger "you can not create object from logger using new keyword , logger provides two method to get instance from it "
    1. public static Logger getRootLogger();
    2. public static Logger getLogger(String name);
  8.  run the project then go to your home you will find the log directory and inside it log.txt
  9. open the file you will find in it the messages 
  10. log4j Resources

    1. http://www.tutorialspoint.com/log4j/index.htm