Spring implicitly provides Jakarta (Apache) Commons Logging API (JCL) support for the client applications. We do not have to include JCL dependency in our projects as it's already included in Spring core module. Spring itself uses JCL internally.
What is JCL?
JCL wraps a third party log implementation around its own API.
We can provide an implementation dependency (like Log4J) in our project. JCL uses a runtime discovery algorithm that looks for a logging frameworks implementation provided in the classpath.
We can also specify an explicit logging implementation in a commons-logging. properties instead of relying on auto discovery mechanism.
By default, JCL uses Java Util logging (JUL) as the log implementation.
In this tutorial, we will see an example of using JCL API and the output from java Util logging (the default).
Post Spring 5 logging changes
Starting Spring 5 a new module called spring-jcl was introduced which replaced old Common Logging dependency. This implementation (spring-jcl) is designed to offer special support for Log4J 2, SLF4J, and java.util.logging.
Example
A Bean using logging
package com.logicbig.example;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class MyBean {
private static Log log = LogFactory.getLog(MyBean.class);
public void doSomething() {
log.info("doing something");
}
}
Main class
package com.logicbig.example;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ExampleMain {
@Bean
public MyBean myBean() {
return new MyBean();
}
public static void main(String[] args) {
ConfigurableApplicationContext context =
new AnnotationConfigApplicationContext(ExampleMain.class);
MyBean bean = context.getBean(MyBean.class);
bean.doSomething();
}
}
OutputNov 10, 2025 12:39:48 PM com.logicbig.example.MyBean doSomething INFO: doing something
Above output shows Java Util (JUL) default logs even though we are using JCL API in our code.
In the next tutorial we will customize default JUL log properties.
Example ProjectDependencies and Technologies Used: - spring-context 6.2.12 (Spring Context)
Version Compatibility: 3.2.3.RELEASE - 6.2.12 Version compatibilities of spring-context with this example: Versions in green have been tested.
- JDK 25
- Maven 3.9.11
|