How to Log with Apache Commons Logging and log4j

1) Create the CommonsSample.java file:

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

public final class CommonsSample {

  public static void main(final String[] args) {
    final LogFactory factory = LogFactory.getFactory();

    final Log log = factory.getInstance(CommonsSample.class);

    log.info("Hello World!");
  }

}

2) Create the commons-logging.properties file:

org.apache.commons.logging.Log=
org.apache.commons.logging.impl.Log4JLogger # default is org.apache.commons.logging.impl.SimpleLog

3) Create the log4j.properties file:

log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout

log4j.logger.CommonsSample=INFO, A1

4) Compile the CommonsSample.java file.

5) Launch the CommonsSample class.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s