Previous we introduced the Spring 2.x default in the Boot logging framework Logback use. Today, we will continue to talk about logs. Next, we will talk about Log4j2, which broke the nuclear bomb vulnerability some time ago. Although a loophole has caused many small partners to suffer for 1-2 weeks (overtime), it is undeniable that Log4j2 is still the best performing logging framework at present. Therefore, when the performance of Logback cannot be supported, it is the fastest and most convenient way to replace Log4j2. Next, let's learn how to replace Logback and use Log4j2 to record logs in Spring Boot 2.x version.
try it out
The basic creation of the Spring Boot project is omitted here. If you haven't already, you can read the Quick Start this tutorial.
The following operations you can continue based on the of the default logging framework Logback in 161d6694b3cf98 Spring Boot 2.x, or you can try it with any Spring Boot 2.x project.
Step : In pom.xml
incorporated in Log4j2 Starter dependent spring-boot-starter-log4j2
, while excluding the default introduced spring-boot-starter-logging
, such as the following:
<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>
second step : In the configuration file application.properties
, configure the location of the log4j2 configuration file through the logging.config
logging.config=classpath:log4j2.xml
third step : Create a new log4j2.xml
in the resource directory (this is not absolute, create it according to the content configured in the second step), and then add the log configuration of log4j2, for example, as follows:
<?xml version="1.0" encoding="UTF-8"?>
<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>
It is mainly to facilitate everyone to understand how to introduce log4j2 into Spring Boot. How to configure log4j2 is not described in detail here, so here is a simple configuration to let the program run. If you want to know more about the configuration of , you can click here
OK, here the integration process is over. Is it very simple? If you encounter difficulties in the learning process? You can join our super high-quality Spring technical exchange group , participate in exchanges and discussions, and learn and progress better! More Spring Boot tutorials can be clicked directly! , welcome to collect and forward support!
common problem
Some friends may ask, didn't you recommend that you use Slf4j to record logs before, to isolate the specific implementation of the log framework? So how do I know that after this operation, I have really used Log4j2?
This is actually a good judgment. You only need to add an endpoint where the log is used, Debug runs, and observe the log object, for example:
The following is the case with default Logback:
The following is the case of using Log4j2
Finally, because Log4j2 has had a lot of loopholes before, you must use the latest version!
To be on the safe side, it is recommended that you at least use a version above 2.17.0 (if you use Spring Boot 2.6.2+, it is already 2.17.0, so don't worry). Of course, the latest version is 2.17.1. You can also upgrade to 2.17.1 to use it. How to upgrade? Or in accordance with this article operation can be described.
code example
The complete project of this article can be viewed in chapter8-2
project 2.x
directory in the following warehouse:
- Github:https://github.com/dyc87112/SpringBoot-Learning/
- Gitee:https://gitee.com/didispace/SpringBoot-Learning/
If you think this article is good, welcome Star
support, your attention is the driving force for me to persist!
Welcome to my public account: Programmer DD. Learn about cutting-edge industry news for the first time, share in-depth technical dry goods, and obtain high-quality learning resources
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。