How to Switch Logging Frameworks in Spring Boot Using SLF4J
This article explains how to seamlessly replace Spring Boot's default Logback logging framework with alternatives like Log4j2 by leveraging the SLF4J facade, covering version requirements, dependency management, configuration files, and detailed XML examples for various log levels.
Spring Boot 2.3.4.RELEASE uses Logback as its default logging implementation. By introducing the SLF4J facade, applications can switch to any underlying logging framework without changing source code.
SLF4J (Simple Logging Facade for Java) abstracts the logging API, allowing the underlying implementation to be swapped transparently. This enables both direct API calls and Lombok @Slf4j annotations to remain unchanged when the concrete logger changes.
To replace Logback with Log4j2, first exclude the default spring-boot-starter-logging dependency from the spring-boot-starter-web module, then add the Log4j2 starter:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- remove springboot default logging framework Logback -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>Then add the Log4j2 starter:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>Place a log4j2-spring.xml (preferred) or log4j2.xml file under src/main/resources. The configuration defines properties, appenders (console, file, rolling files), and loggers for specific packages, as shown in the example XML below.
<configuration monitorInterval="5">
<Properties>
<property name="LOG_PATTERN" value="%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"/>
<property name="FILE_PATH" value="your/log/path"/>
<property name="FILE_NAME" value="your-project-name"/>
</Properties>
<appenders>
<console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${LOG_PATTERN}"/>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</console>
<File name="Filelog" fileName="${FILE_PATH}/test.log" append="false">
<PatternLayout pattern="${LOG_PATTERN}"/>
</File>
<RollingFile name="RollingFileInfo" fileName="${FILE_PATH}/info.log" filePattern="${FILE_PATH}/${FILE_NAME}-INFO-%d{yyyy-MM-dd}_%i.log.gz">
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
<DefaultRolloverStrategy max="15"/>
</RollingFile>
<!-- similar RollingFile definitions for WARN and ERROR -->
</appenders>
<loggers>
<logger name="org.mybatis" level="info" additivity="false">
<AppenderRef ref="Console"/>
</logger>
<logger name="org.springframework" level="info" additivity="false">
<AppenderRef ref="Console"/>
</logger>
<root level="info">
<appender-ref ref="Console"/>
<appender-ref ref="Filelog"/>
<appender-ref ref="RollingFileInfo"/>
<appender-ref ref="RollingFileWarn"/>
<appender-ref ref="RollingFileError"/>
</root>
</loggers>
</configuration>Adjust the FILE_PATH and FILE_NAME properties to match your environment. After rebuilding the project, Spring Boot will use Log4j2 for logging while the application code continues to use SLF4J APIs unchanged.
The article concludes by encouraging readers to follow the author for more Spring Boot and MyBatis tutorials.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
