This example shows how to use log4j 2 with JCL API.
As JCL does not have a ready to use adapter for log4j 2, we need to add the bridge dependency log4j-jcl, along with log4j 2 dependency.
<dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-jcl</artifactId> <version>2.8.2</version> </dependency>
appenders=xyz appender.xyz.type = Console appender.xyz.name = myOutput appender.xyz.layout.type = PatternLayout appender.xyz.layout.pattern = %d{yy-MM-dd HH:mm:ss:SSS} %-5p %c{1} [%L] - %m%n rootLogger.level = info rootLogger.appenderRefs = abc rootLogger.appenderRef.abc.ref = myOutput
package com.logicbig.example; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.impl.Log4JLogger; public class Example1 { private static Log log = LogFactory.getLog(Example1.class); public static void main(String[] args) throws IllegalAccessException, InstantiationException { log.info("in the main method"); } }
17-07-04 13:08:54:079 INFO Example1 [19] - in the main method
Dependencies and Technologies Used: