Springboot later versions continue to use the perfect solution of log4j, springbootlog4j

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.