SpringBoot log principle, log implementation

SpringBoot log principle, log implementation, log application

  • Log facade (abstract layer of logs); logging-abstract.jar
    can import specific log implementations into the project; our previous log frameworks are all implemented abstract layers

  • Log framework on the market
    Insert picture description here

  • Log facade selection: SLF4J (one of the other two log facades is not suitable, and the other has not been updated for a long time)

  • Log implementation choice: Logback

  • SpringBoot: The bottom layer uses the Spring framework, and the Spring framework uses JCL by default, and SpringBoot uses SLF4J and Logback

How to use SLF4j in the system

In future development, the call of the logging method should not directly call the log implementation class, but call the method in the abstract layer of the log.

  • Import the implementation jar of slf4j and logback into the system
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld {
    
    
  public static void main(String[] args) {
    
    
    Logger logger = LoggerFactory.getLogger(HelloWorld.class);
    logger.info("Hello World");	
  }
}

Icon:
Insert picture description here

Each log implementation framework has its own configuration file. After the configuration file uses slf4j, the configuration file uses the log to implement the framework's own configuration file ;

Remaining problem

Different frameworks use different log implementations
Insert picture description here

How to unify all logs in the system to slf4j

  1. Exclude all other log frameworks in the system first
  2. Replace the original log framework with an intermediate package
  3. We import other implementations of slf4j

Insert picture description here
to sum up:

  • The bottom layer of SpringBoot also uses slf4j + logback for logging.

  • SpringBoot also replaced other logs with slf4j

  • The replacement package is used in the middle to realize the effect of civet cat for prince

  • If we want to introduce other frameworks, we must remove the log dependency of this framework. The introduction of SpringBoot into the Spring framework also removes the log dependency of the Spring framework.

  • The SpringBoot framework can automatically adapt to all logs, and the bottom layer uses slf4j + logback to record logs. When introducing other frameworks, you only need to remove the log dependencies that this framework depends on.

Log usage

Insert picture description here

 Logger logger = LoggerFactory.getLogger(ReviewerApplicationTests.class);
    @Test
    public void loggerTest(){
    
    

        logger.trace("trace级别日志");
        logger.debug("debug级别日志");
        logger.info("info级别日志");
        logger.warn("warn级别日志");
        logger.error("error级别日志");
    }
  • The default is the log of the info sector
#修改日志级别
logging.level.com.ebusiness.reviewer=trace
#设置日志输出位置,不指定路径,就在当前项目下生成,也可以指定路径
logging.file.path=springBoot.log

logging.file.path=/Users/apple/Desktop

Insert picture description here

Insert picture description here

  • You can also set the format of log printing
    Insert picture description here
    Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43941676/article/details/108608713
log
log