Springboot later versions continue to use the perfect solution of log4j, springbootlog4j
Springboot later versions do not support log4j. Many people prefer log4j-style logs. We can load log4j by ourselves, which is actually very easy.
Step 1: manually add the log4j jar we want, create a folder in the project, drop the jar used, right-click add to build path
Step 2:
Write such a class in the package or its sub-package where the main function startup class is located to load the log4j configuration file. Yes, there is nothing.
import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.ComponentScan;@ComponentScan@ConfigurationProperties("classpath:log4j.properties")public class Log4jConfigure {}
Here, a yellow warning may appear, prompting you to add it to the pom file.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
Click OK to add the following dependencies.
Step 2: Drop the log4j. properties file next to the application. properties configuration file,
The method used in controller is exactly the same as before,
Import com. dome. dao. userMapper; import com. entity. user; @ RestController @ RequestMapping ({"/home"}) public class UserController {private static Logger log = Logger. getLogger (UserController. class); log. debug ("the default user is successfully loaded by debug"); log.info ("the default user is successfully loaded"); log. error ("encountered error, rollback successful ")}
Next, we configure the log output of mybatis to log4j.
Add a mybatis-config.xml file next to application. properties and fill in the following content
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <settings> <setting name="logImpl" value="LOG4J"/></settings></configuration>
Next, open application. properties and add the following line of information. The SQL statement can be output to the console.
Mybatis. config-location = classpath: mybatis-config.xml
Summary
The above is the perfect solution for using log4j after springboot is introduced in the small series. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!