Apache Commons Logging + Simple Log Example

[Updated: Jul 4, 2017, Created: Jul 4, 2017]

This example shows how to use Simple Log with JCL. Simple Log implementation is provided by JCL itself so we don't have to include any implementation dependencies.

Dependencies

pom.xml

<dependency>
   <groupId>commons-logging</groupId>
   <artifactId>commons-logging</artifactId>
   <version>1.2</version>
</dependency>

JCL configuration

In commons-logging.properties, we have to specify SimpleLog as Log implementation:

src/main/resources/commons-logging.properties

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

Using JCL API

package com.logicbig.example;

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

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

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

Output

[INFO] ExampleClass - info in the main method
[ERROR] ExampleClass - error in the main method

Simple log configurations

check out the configuration details here.

src/main/resources/simplelog.properties

org.apache.commons.logging.simplelog.defaultlog=error
org.apache.commons.logging.simplelog.showdatetime=true
org.apache.commons.logging.simplelog.dateTimeFormat=yyyy-MM-dd HH:mm:ss:SSS
package com.logicbig.example;

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

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

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

Output

2017-07-04 13:59:22:189 [ERROR] ExampleClass - error 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 + SimpleLog Example Project Select All Download
  • jcl-simple-log-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources

See Also