Use of logs

1 What is a logging framework

  1. It is a toolkit that can realize log output

  2. All the time and content that can describe the operating state of the system can be counted as logs

The ability of the two-logging framework

  1. Customized output capability

  2. Customize the output format

  3. Carry contextual information

  4. Selective output at runtime

  5. Flexible configuration

  6. Excellent performance

  These features are not available in System.out

Three common logging frameworks

  Log Framework Classification: Log Facade and Log Implementation

  Log facade: JCL, SLF4J, jboss-loggins

  Log implementation: Log4j, Log4j2, Logback

  Which one do we use with so many frameworks? It’s actually quite simple. Log4j, Log4j2 and Logback are the same author. The author thinks that Log4j is too bad and doesn’t want to be modified, so he directly rewrites a Logback. On the other hand, because the design of Log4j2 is too advanced, we usually do not use many functions, so it is not recommended to use it. The author of SLF4J is also the author of Logback, and many frameworks use the combination of SLF4J + Logback, so it is currently the best combination of log frameworks

Four SLF4J and Logback use

  The following test code is written using idea

  1. Basic usage

// Unit test based on springboot 
@RunWith( SpringJUnit4ClassRunner.class )
@SpringBootTest
public  class LoggerTest {
     // Get the log instance 
    private Logger logger = LoggerFactory.getLogger(LoggerTest.class ) ;

    @Test
    public  void test1() {
         // output log 
        logger.debug( " debug... " );
        logger.info("info...");
        logger.error("error...");
    }

}

  An obvious drawback of this usage is that each class that needs log output must be written once

private Logger logger = LoggerFactory.getLogger(LoggerTest.class);

   Very troublesome, the following uses an annotation method to improve the code

  2. Annotation usage

  First add the lombok dependency

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

  Then add the @Slf4j annotation to the class (if your idea version is too low, the log variable will not be found, you need to install the lombok plugin)

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
// Add this annotation to 
@Slf4j
 public  class LoggerTest {

    @Test
    public  void test2() {
         // The name of the log object has changed to log 
        log.debug( " debug... " );
        log.info("info...");
        log.error("error...");          
    }
}

  3. Output variables in the log

@Slf4j
public class LoggerTest {
    @Test
    public  void test3() {
         // General writing 
        String name = " logback " ;
        String password = "123";
        log.info( " General writing name: " + name + " , password: " + password);

        // Placeholder writing, use {} placeholder instead of variable 
        log.info( " placeholder writing name:{}, password:{} " , name, password);
    }
}

  4. Logback configuration

    The configuration of logback can be divided into property file configuration and xml file configuration. The former has simple configuration and simple functions, and the latter is vice versa.

    Simple configuration in the property file application.properties

#Log output format configuration
logging.pattern.console=%d - %msg%n
#Configure log path
logging.path=/var/log/tomcat/
#Configure the log file name under the log path
logging.pattern.file=/var/log/tomcat/sell/
#Configure log level
logging.level.root=debug
#Configure the log level of the class
logging.level.top.pancras.SellApplication=error

  For more configuration based on xml, refer to the official website and other blogs...

  https://logback.qos.ch/manual/configuration.html

  https://www.cnblogs.com/warking/p/5710303.html

 

Guess you like

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