Springboot does not support log4j after the higher version. Many people still like log4j-style logs. We can load log4j ourselves, which is actually very easy.
Step 1: We manually add the log4j jar we want, build a folder in the project, throw the used jar in, right-click to add to build path
Step 2:
Write a class like this in the package or its subpackage where the main function starts the 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 to prompt you to add it in the pom file.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
If you click OK, it will automatically add it to you.
Step 2: Just throw the log4j.properties file next to the application.properties configuration file, and you don’t have to do anything else.
The usage 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("debug loads the default user successfully"); log.info("Loads the default user successfully"); log.error("Error encountered, rollback successfully")}Next we configure mybatis' log output 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>
Then open application.properties, add the following line of information, and the SQL statement can be output to the console.
mybatis.config-location=classpath:mybatis-config.xml
Summarize
The above is the perfect solution for continuing to use log4j after the higher version of springboot, I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!