Close

Apache Commons Logging features and a quick start example

[Last Updated: Jul 4, 2017]
  • A logging API without implementation:

    Apache Commons Logging (JCL) provides a logging API which can be used with any logging implementation library like log4j. Developer logs messages with JCL API but during runtime any implementation can be set up to be used.

  • What is the API?

    A developer should know only two classes:
    • LogFactory A factory for creating Log instances.
    • Log: The main interface for logging messages with desired priority (the logging level).


  • How it works:

    JCL uses a runtime discovery algorithm that looks for a logging implementation in the classpath. Once an implementation found, a corresponding adapter is used to forward logging calls to that specific implementation. These adapters are nothing but the implementations of org.apache.commons.logging.Log (the main logging interface, as mentioned above). In JCL version 1.2 followings adapters are available: Note that SimpleLog is JCL's own implementation.


  • Bridges:

    Other than above ready to use adapters, an external adapter can also be hooked up by providing a bridge dependency. This mechanism allows us to use whatever logging implementation we want to. The bridge should provide both a LogFactory and Log implementation. We just have to make JCL aware of our LogFactory implementation as described in the API docs (Check out "Choosing a LogFactory Implementation" section).
    Log4j 2 provides the similar bridge via log4j-jcl.jar:

  • What is the default?

    If no implementations of a logging framework is available then Jdk14Logger is used, which forwards logging messages to Java Util logging (JUL).

  • Writing new adapters:

    New adapters can also be written and hookup easily.

Quick getting started examples

In this example we are not going to provide no logging framework implementation, so the default (JUL) will be used.

package com.logicbig.example;

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

public class Example1 {
  private static Log log = LogFactory.getLog(Example1.class);

  public static void main(String[] args) {
      log.info("in the main method");
  }
}

Output

Jun 23, 2017 7:45:45 PM com.logicbig.example.Example1 main
INFO: in the main method

In the following example, we are going to modify JUL default logging format:

package com.logicbig.example;

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

public class Example2 {
  private static Log log = null;

  static {
      System.setProperty("java.util.logging.SimpleFormatter.format",
              "[%1$tF %1$tT] [%4$s] %5$s %n");
      log = LogFactory.getLog(Example2.class);
  }

  public static void main(String[] args) {
      log.info("in the main method");
  }
}

Output

[2017-06-23 19:49:22] [INFO] in the main method 

Example Project

Dependencies and Technologies Used:

  • commons-logging 1.2: Apache Commons Logging is a thin adapter allowing configurable bridging to other, well known logging systems.
  • JDK 1.8
  • Maven 3.3.9

Jcl Getting Started Example Select All Download
  • jcl-default-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • Example1.java

    See Also