In the previous article we introduced the use of the default logging framework Logback in Spring Boot 2.x. Today we continue to talk about logs, next we are going to talk about the Log4j2 nuclear bomb vulnerability exposed some time ago. There is no denying that Log4j2 is still the best logging framework in the world. Therefore, when Logback performance is not supported, using Log4j2 instead is the fastest and easiest way. Let’s learn how to log using Log4j2 instead of Logback in Spring Boot 2.x.

Give it a try

The basics of creating a Spring Boot project are omitted here, so if you don’t already have a quick start with this tutorial.

You can continue with the example in the Spring Boot 2.x default logging framework Logback article, or you can try it with any Spring Boot 2.x project.

The first step is to introduce the Log4j2 Starter dependency on spring-boot-starter-log4j2 in pm. XML and exclude spring-boot-starter-logging by default, as shown in the following example:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
Copy the code

Step 2: In the configuration file application.properties, specify the log4j2 configuration file location through the logging.config configuration, as follows:

logging.config=classpath:log4j2.xml
Copy the code

Step 3: Create log4j2. XML in the resource directory (this is not absolute, based on the configuration in step 2) and add the log configuration for log4j2, for example:


      
<Configuration status="INFO">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>
Copy the code

Log4j2 to Spring Boot. We won’t go into details on how to configure log4j2, so we’ll just put a simple configuration to get the program running. For an in-depth look at the log4j2 configuration, click here

OK, that’s the end of the integration process. Is it easy? If you have any difficulty in learning? You can join our super high quality Spring technology exchange group, participate in the exchange and discussion, better learning and progress! More Spring Boot tutorials can be clicked direct! , welcome to collect and forward support!

Q&A

Slf4j is recommended for logging, isolating the implementation of the logging framework. So how do I know that I’m actually using Log4j2 after this operation?

This is easy to determine. You just need to add an endpoint where the log is used, Debug runs, and observe the log object. For example:

Here is a case where the default Logback is used:

So this is the case with log base 4j2

Finally, because Log4j2 has a big bug, you must use the latest version!

To be safe, it is recommended that you use at least 2.17.0 (if you are using Spring Boot 2.6.2+, it is already 2.17.0, so don’t worry). Of course, the latest is 2.17.1, you can also upgrade to 2.17.1 to use, how to upgrade? Or follow the operation introduced in this article.

Code sample

For the complete project of this article, see the chapter8-2 project in the 2.x directory of the warehouse below:

  • Github:github.com/dyc87112/Sp…
  • Gitee:gitee.com/didispace/S…

If you found this article good, welcomeStarSupport, your attention is my motivation!

Welcome to pay attention to my public number: program ape DD. Learn cutting-edge industry news, share in-depth technical know-how, and obtain high-quality learning resources at the first time