Log4j Configuration in Java project

By | November 6, 2014

Apache log4j is logging library for Java which is distributed under the Apache Software License. In this post we will learn log4j configuration in a sample java project using eclipse IDE.

You can download the latest version of log4j.jar from hereIf the link does not work copy and paste the below URL in the browser and download.

http://logging.apache.org/log4j/1.2/download.html

The other jar file needed for this is commons-logging.jar you can download it from here. If the link does not work copy and paste the below URL in the browser and download.

http://commons.apache.org/proper/commons-logging/download_logging.cgi

Log4j Configuration in Eclipse IDE :-

1. First we need to create a java project with name Log4jExample in the Eclipse IDE. 

2. In this sample I am using the log4j-1.2.8.jar and commons-logging-1.0.4.jar.

3. Configure the Build path for the project and add the log4j.jar, commons-logging.jar to the libraries

4. Then create log4j.properties file and commons-logging.properties file as shown below and place them under src folder.

log4j.properties file :-

log4j.rootLogger=ERROR, A1
log4j.logger.com.examples=DEBUG
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.file=log4jExample.log
log4j.appender.A1.datePattern='.'yyyy-MM-dd-HH
log4j.appender.A1.append=true
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d %5p (%F:%L) - %m%n

commons-logging.properties file :-

org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.Log4jFactory

5. Then create a java class with name Log4jExample.java and place it in a package com.examples as shown below.

Sample Java program including the log4j :-

package com.examples;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Log4jExample {

 private static final Log LOG = LogFactory.getLog(Log4jExample.class);
 /**
  * @param args
  */
  public static void main(String[] args) {
      // TODO Auto-generated method stub
      LOG.debug("This is sample Logging.");
      System.out.println("This is sample Logging.");
 }

}

The complete folder structure for the java project should be same as shown in the image below. log4j Configuration

 

The output will be written in a log file as shown below.

2014-11-04 18:08:41,295 DEBUG (Log4jExample.java:14) - This is sample Logging.
2014-11-04 18:08:50,484 DEBUG (Log4jExample.java:14) - This is sample Logging.

The org.apache.log4j.DailyRollingFileAppender in log4j.properties files will create new log file daily and rename old file with previous day date.

If you bundle your application as Jar then  create a lib folder in the jar file and place the  log4j.jar  and commons-logging.jar files inside the lib folder and add the lib, log4j.jar and commons-logging.jar  to the classpath variable in the MANIFEST.MF file as shown below.

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: 1.6.0_02-b06 (Sun Microsystems Inc.)
Class-Path: lib lib/log4j-1.2.8.jar lib/commons-logging-1.0.4.jar

Enjoy Happy coding….

Leave a Reply

Your email address will not be published. Required fields are marked *