Apache Commons Logging + log4j Example

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

This example shows how to use log4j with JCL API.

Dependencies

As JCL provides an adapter for log4j (Log4JLogger), we don't need any bridge dependency, just log4j dependency is enough.

pom.xml

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

Log4j configuration

src/main/resources/log4j.properties

log4j.rootCategory=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{yy-MM-dd HH:mm:ss:SSS}] [%p] %t %c{1}:[%L] - %m%n

Using JCL API

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

[17-07-04 11:53:43:633] [INFO] main{1} Example1:[10] - 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.
  • log4j 1.2.17: Apache Log4j 1.2.
  • JDK 1.8
  • Maven 3.3.9

JCL + Log4j Example Project Select All Download
  • jcl-with-log4j
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources

See Also