Just like JCL, SLF4J is another wrapper API for different logging providers. SLF4j has various advantages over JCL (quick features here). This example will demonstrate how to use SLF4J with Log4j2. Here Log4j2 will be an implementation provider for SLF4j.
Maven dependencies
pom.xml<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.2.12</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.25.2</version>
</dependency>
The log4j-slf4j-impl dependency binds the SLF4J API to Log4j implementation. It also pulls Log4j and slf4j dependencies so we don't have to include that separately.
A Bean using SLF4J API
package com.logicbig.example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyBean {
private static Logger log = LoggerFactory.getLogger(MyBean.class);
public void doSomething() {
log.info("doing something");
}
}
log4j2.properties
src/main/resources/log4j2.propertiesstatus = error
name = PropertiesConfig
filters = threshold
filter.threshold.type = ThresholdFilter
filter.threshold.level = info
appenders = console
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yy-MM-dd HH:mm:ss:SSS} %-5p %c{1}:%L - %m%n
rootLogger.level = info
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
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();
}
}
Output25-11-11 14:15:29:807 INFO MyBean:10 - doing something
Before Spring 5
As we mentioned before in our previous tutorials that spring 5 provides it's own JCL bridge, that's why we didn't have to exclude JCL layer in this example. Starting Spring 5, JCL bridge routes all logs to SLFJ if its dependency is provided in classpath.
Before Spring 5 we had to explicit exclude common logging dependency and add the routing from JCL to SLF4J manually. See this complete example for pre Spring 5 way of using SLF4j.
Example ProjectDependencies and Technologies Used: - spring-context 6.2.12 (Spring Context)
Version Compatibility: 5.0.0.RELEASE - 6.2.12 Version compatibilities of spring-context with this example: Versions in green have been tested.
- log4j-slf4j-impl 2.25.2 (SLF4J 1 binding (provider) for the Log4j API.
It forwards SLF4J 1 calls to the Log4j API.
(Refer to the `log4j-to-slf4j` artifact for forwarding the Log4j API to SLF4J.))
- JDK 25
- Maven 3.9.11
|