A simple example of log4j

In this article we will discuss how to generate log statement in a java program.

Tools Used

  • JDK 1.6
  • Eclipse Indigo

JAR files required for this application

  • log4j-1.2.15.jar

Let us start with our first log4j application.


Step 1: Create Java project

Open Eclipse and goto File -> New -> Project and select Java Project in the New Project wizard screen and click Next.

In this article we will discuss how to generate log statement in a java program.

Tools Used

  • JDK 1.6
  • Eclipse Indigo

JAR files required for this application

  • log4j-1.2.15.jar

Let us start with our first log4j application.


Step 1: Create Java project

Open Eclipse and goto File -> New -> Project and select Java Project in the New Project wizard screen and click Next.


log4j basic example

log4j basic example

Provide the name of the project as BasicLog4jExample and click on Finish.

log4j basic example

Step 2: Add jar files to build path of the project

For simplicity we are going to create lib folder to accommodate necessary jar files required for this project. Creating new folder in eclipse is too easy,just right click on Project and select -> New -> folder and provide the name of the folder as lib. After creation of the folder copy the log4j-1.2.15.jar into this folder and add this jar file to the classpath of the project.

log4j basic example

Step 3: Create Java class

Create a package com.jwt.log4j and create a java class HelloWorld inside this package. Add following lines of code in this class.

HelloWorld.java

package com.jwt.log4j;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

public class HelloWorld {

	static final Logger logger = Logger.getLogger(HelloWorld.class);

	public static void main(String[] args) {
		BasicConfigurator.configure();
		logger.debug("Sample debug message");
		logger.info("Sample info message");
		logger.warn("Sample warn message");
		logger.error("Sample error message");
		logger.fatal("Sample fatal message");
	}
}


Description of the code:

  • Logger.getLogger(): Logger class is used for handling the majority of log operations and getLogger method is used for return a logger according to the value of the parameter. If the logger already exists, then the existing instance will be returned. If the logger is not already exist, then create a new instance.

  • Logger.debug(): This method is used to check that the specified category is DEBUG enabled or not, if yes then it converts the massage passed as a string argument to a string by using appropriate object renderer of class ObjectRenderer.

  • Logger.info(): This method is used to check that the specified category is INFO enabled or not, if yes then it converts the massage passed as a string argument to a string by using appropriate object renderer of class ObjectRenderer.

  • Logger.warn(): This method is used to check that the specified category is WARN enabled or not, if yes then it converts the massage passed as a string argument to a string by using appropriate object renderer of class ObjectRenderer.

  • Logger.error(): This method is used to check that the specified category is ERROR enabled or not, if yes then it converts the massage passed as a string argument to a string by using appropriate object renderer of class ObjectRenderer.

  • Logger.fatal(): This method is used to check that the specified category is FATAL enabled or not, if yes then it converts the massage passed as a string argument to a string by using appropriate object renderer of class ObjectRenderer.

  • BasicConfigurator. configure() method will log all the messages on the console.

Output

0 [main] DEBUG com.jwt.log4j.HelloWorld  - Sample debug message
1 [main] INFO com.jwt.log4j.HelloWorld  - Sample info message
2 [main] WARN com.jwt.log4j.HelloWorld  - Sample warn message
2 [main] ERROR com.jwt.log4j.HelloWorld  - Sample error message
2 [main] FATAL com.jwt.log4j.HelloWorld  - Sample fatal message

You can download the source code of the example by clicking on the Download link below.

Source : Download
Source + Lib : Download





comments powered by Disqus