springboot Shangguigu study notes 03

------spring boot and logs------

  Log framework:

  

  Logging frameworks on the market:

    jul jcl jboss-logging logback log4j log4j2 ......

  

  There is a facade (abstract layer) on the left, and an option is selected on the right;

  Log Facade Choice: SLF4j

  Log implementation: Logback

  spring boot: the bottom layer is the spring framework, and the bottom layer of spring uses jcl by default;

    Spring boot chooses slf4j and logback

  How to use SLF4j

  When developing the invocation of the logging method, the implementation class of the log should not be called, but the method of the abstract layer of the log should be called.

SLF4j jar and logback implementation jar  should be added to the system

  https://www.slf4j.org/manual.html

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");
  }
}

  SLF4J use legend: which jar needs to be imported to use log, etc.

  Each log implementation framework has its own configuration file. After using slf4j , the configuration file is still made into the configuration file of the log implementation framework.

 

  Remaining problem:

  a项目 (slf4j + logback): srping(common-logging, Hibernate(jboss-logging), mybatis 。。。。。。)

  Unified logging even if other frameworks use slf4j in unity with me 

  How to realize the following picture: use the conversion package of other jar-2-slfj for each log jar package and then call slf4j uniformly

    How to unify all logs in the system to slf4j:

      1. Exclude other log frameworks in the system

      2. Replace the original logging framework with an intermediate package

      3. Let's import other implementations of slf4j

  Log and springboot relationship: springboot project dependencies

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.0.1.RELEASE</version>
      <scope>compile</scope>
</dependency>

  spring-boot-starter dependency: spring-boot uses it for logging

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-logging</artifactId>
      <version>2.0.1.RELEASE</version>
      <scope>compile</scope>
</dependency>

  View the dependency graph through the pom file:

  Spring boot underlying dependencies:

  Summarize:

    The bottom layer of spring boot also uses slf4j + logback for logging

    spring boot also replaces other logs with the slf4j package

    Intermediate replacement package : jcl - "slf4j implementation is slf4j 

    If we want to introduce other frameworks, we must remove the default log dependencies of this framework, otherwise there will be conflicts

    Spring boot can automatically adapt to all logs. The bottom layer uses slf4j + logback to record logs. When other frameworks are introduced, their log dependencies must be excluded.

  log instance:

  Spring boot has enabled the log function by default:

  test:

package com.lixuchun.springboot;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot03LoggingApplicationTests {

    // Logger 
    Logger logger = LoggerFactory.getLogger(getClass());

    @Test
    public void contextLoads() {

        // The level of the log
         // From low to high trace<debug<info<warn<error
         // The level of the log can be adjusted The log will only print the log of this level or higher 
        logger.trace("This is the trace trace" ) ;
        logger.debug( "This is the debug log" );
         // springboot uses the info level by default 
        logger.info("This is the info log" );
        logger.warn( "This is the warn log" );
        logger.error( "This is the error log" );
    }

}

  Set the level instance in the properties file:

  %d{yyyy-mm-dd}  [%thread]   %-5level      %logger{50}    %msg%n

  2018-02-01 [main] Left-aligned Trace 50 log characters, log information wraps

 

  2 Designated placement

    Put the configuration file of each logging framework on the classpath, and springboot does not apply its default configuration.

  logback.xml : directly recognized by the logging framework

  logback-spring.xml : The logging framework is not loaded directlyby  springboot

  spring.profiles.active=dev activates the dev profile

  or the console to activate

  Then you can use the advanced features of springboot

 

  Switch logging framework: pom file remove 1 2 add 3 

    Removal of pom dependencies through the pom dependency tree is concise

  Then use the logging framework of log4j2

 

   Previous: spring boot Shangguigu study notes 02

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325068669&siteId=291194637