Log4j2 Enhancements in Spring Boot 3
3 min readJul 4, 2023
Spring Boot 3.0 introduces some enhancements to Log4j2. These improvements include:
- Profile-specific configuration: This allows developers to create Spring profile-specific custom configurations for Log4j2. This is useful for configuring different logging levels or output formats for different environments.
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="logback-spring.xsd">
<springProfile name="dev">
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.springboot.postgres" level="debug">
<appender-ref ref="console" />
</logger>
</springProfile>
<springProfile name="prod">
<appender name="file" class="ch.qos.logback.core.FileAppender">
<file>app.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.springboot.postgres" level="info">
<appender-ref ref="file" />
</logger>
</springProfile>
</configuration>
This code snippet shows how to configure two different Log4j2 settings, one for the dev profile and one for the prod profile. The dev profile uses the console appender to set the messages to the console, while…