Log4j Example

In this article, you will see very basic logging configuration.

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

  <dependencies>
	  <dependency>
		<groupId>log4j</groupId>
		<artifactId>log4j</artifactId>
		<version>1.2.17</version>
	  </dependency>
  </dependencies>  

Log4j in Java Program

The following Java class is a very simple example that initializes, and then uses, the Log4J logging library for Java applications. Use BasicConfigurator.configure() method which logs all the messages on the console.

package com.kruders;

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.info("Hello World!!!");
	}
}

When you run the above example you’ll get an output like:

0 [main] INFO com.kruders.HelloWorld – Hello World!!!

You can download the source code of this example here.


,

No comments yet.

Leave a Reply