Logfactory

org.apache.commons.logging.logfactory Example

In this example, we shall be talking about how we can use the Apache commons org.apache.commons.logging.Logfactory class.

You can download the apache commons logging Jar file from here.

The Logfactory class uses the Factory Design Pattern to select a logger class. The Apache Commons Logging library decouples the code from the underlying logging library we use.

 
 

The Logger class can be any one from these:

  1. org.apache.commons.logging.impl.Log4JLogger
  2. org.apache.commons.logging.impl.Jdk14Logger
  3. org.apache.commons.logging.impl.SimpleLog

We shall be using Log4J as the underlying logging library for the sake of this example.

Implementation:

We need to have the commons-logging.properties in our src folder.

commons-logging.properties:


org.apache.commons.logging.Log = org.apache.commons.logging.impl.Log4JLogger

The above property tells the Commons Library as to which Logger Class, the application intends to use. Then, we need to set up the corresponding library, Log4J in this case. The details about log4j setup are discussed, here. We will discuss in brief, how we can use it with Apache Commons Logging.

We need to add the log4j.properties to the src folder.

log4j.properties:

log4j.rootLogger=DEBUG, file

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\logger.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Now, with both the libraries configured we can use them in our Class:

LogDemo.java:


package com.jcg.examples;

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


public class LogDemo
{
		private static Log logger = LogFactory.getLog(LogDemo.class);

		public static void main(String[] args)
    {
				logger.info("Test info");
				logger.debug("Test info");

    }
}

logger.log:

2015-01-30 23:10:27 INFO  LogDemo:14 - Test info
2015-01-30 23:10:27 DEBUG LogDemo:15 - Test info

The commons-logging.properties file configures the application to use the log4j library using the Log4JLogger Class. When we intend to use any other implementation of Logging, we just need to provide the appropriate class name in the commons-logging.properties file.

Since our code(i.e. Java Classes) does not use any specific classes, they remain decoupled and unaffected by changes made to the underlying logging mechanism, as should be the ideal case!

Conclusion:

We studied how we can configure any library to be used without changing the existing code using the Apache Commons Logging library.

Download
You can download the source code of this example here: CommonLoggingDemo.zip

Chandan Singh

Chandan holds a degree in Computer Engineering and is a passionate software programmer. He has good experience in Java/J2EE Web-Application development for Banking and E-Commerce Domains.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button