Log4j2 Enhancements in Spring Boot 3

Saurav Kumar
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 the prod profile uses the file appender to set the messages to a file. The logger level of the com.springboot.postgres logger is also set to debug data in the dev profile and info in the prod profile.

  • Environment object monitoring: This allows developers to view properties in the Spring environment in the Log4j2 configuration.
    This is useful for configuring Log4j2 based on runtime values ​​such as application name or current environment.
<?xml version="1.0" encoding="UTF-8"?>
<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="${logging.level:-debug}">
<appender-ref ref="console" />
</logger>
</springProfile>
</configuration>

This code snippet shows you how to configure a Log4j2 appender that uses the logging.level environment property to set the logger level. The logging.level property is set to debug by default, but can be overridden by setting it in the application’s environment.

  • Log4j2 System Properties: This allows developers to configure Log4j2 using system properties. This is useful for configuring Log4j2 without modifying the application code.
<?xml version="1.0" encoding="UTF-8"?>
<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="${sys:logging.level:-debug}">
<appender-ref ref="console" />
</logger>
</springProfile>
</configuration>

This code snippet shows you how to configure the Log4j2 appender using the logging.level system property to set the log level. The logging.level property is set to debug by default but can be overridden by setting it on the command line when the application starts.

NOTE: The above code snippets should be in the logback-spring.xml file of the Spring Boot application. This file is used to configure Log4j2 for your application. The logback-spring.xml file is usually located in the src/main/resources directory of a Spring Boot application.

These improvements help developers configure Log4j2 in Spring Boot applications more easily and flexibly. In addition to these improvements, Spring 3. Spring Boot 3.0 also includes many other improvements for Log4j2 such as:

Improved support for Log4j2 2.17.0: Spring Boot 3.0 includes new updates to support the latest version of Log4j2.

Fixed a bug that prevented Log4j2 from working with Spring Boot’s Runner endpoint: this bug was introduced in Spring Boot 2.6.0 and prevents users from viewing the registry using Spring Boot’s Actuator endpoint.

Improved documentation for configuring Log4j2.

Overall, Spring 3.0 brings many improvements and improvements to Log4j2. These improvements help developers more easily and flexibly install Log4j2 into their Spring applications.

Source code reference

The source code for the examples in this blog can be found on GitHub:

https://github.com/sauravkumarshah/spring-boot-with-postgresql

--

--

Saurav Kumar
Saurav Kumar

Written by Saurav Kumar

Experienced Software Engineer adept in Java, Spring Boot, Microservices, Kafka & Azure.

Responses (1)