Programmers must have skills - use logs

Table of contents

1. Why use logs

 2. Custom log printing

2.1. Get the log object in the program

 2.2, use the log object to print the log

 2.3, log format

3. Log level

3.1. Classification of log levels

3.2, log level settings

4. Persistent logs

 5. Simpler log output - lombok

5.1. How to add dependencies to the created SpringBoot project 

5.2. Code changes:

 5.3 Explanation of lombok principle


1. Why use logs

  • Ability to discover and locate problems
  • Able to record user login logs, so as to analyze whether the user logs in normally or maliciously cracks the user
  • It can record the operation log of the system, which is convenient for data recovery and locating the operator
  • Record the execution time of the program to provide data support for future optimization programs

For example, when the SpringBoot project starts, the output console log information:


 2. Custom log printing

step:

  • Get the log object in the program
  • Output what to print using the relevant syntax of the log object

2.1. Get the log object in the program

//1、得到日志对象
private static Logger logger = LoggerFactory.getLogger(ControllerTest.class);

 Note: The Logger object belongs to the org.slf4j package

Because the log framework slf4j is built into SpringBoot, we directly call slf4j in the program to output the log

How to understand this question?

        In other words, there are many ways to implement logs, and the skills mastered by developers may not be uniform, which will cause each programmer to implement logs in different ways. When a programmer leaves , another programmer wants to modify some log output format and other things, it will be very troublesome, so the facade of the log works here, and the decoupling is successfully achieved. The developer only needs to be responsible for calling the docking slf4j, Instead of paying attention to how the log is implemented

 2.2, use the log object to print the log

package com.example.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:龙宝
 * Date:2023-03-13
 * Time:15:40
 */
@RestController
@RequestMapping("/test")
public class ControllerTest {
    //1、得到日志对象
    private static Logger logger = LoggerFactory.getLogger(ControllerTest.class);
    @RequestMapping("/hi")
    public String sayHi() {
        //写日志
        logger.trace("日志:trace");
        logger.debug("日志:debug");
        logger.info("日志:info");
        logger.warn("日志:warn");
        logger.error("日志:error");
        return "日志打印";
    }

}

        After starting the project, enter the corresponding address in the url, after visiting, return to IDEA, and you will see the log you defined~

        In order to see clearly, we can delete the log printed when the Spring Boot project starts:

access:

 

log:

 

 2.3, log format

Log attribution, which may also be an abbreviation, such as: 

The first two abbreviations~ 

        Some friends may see that the printed logs are not complete. It is obvious that five are written, but only three are printed. Why? Here will lead to the level of the log~


3. Log level

3.1. Classification of log levels

  • trace: trace, a little, the lowest level
  • debug: Print key information when debugging is required
  • info: common print information (default log level)
  • warn: warning, does not affect the use, but needs attention
  • error: error information, higher level error log information
  • fatal: Fatal, because the code exception causes the program to exit the execution event

Order of log levels:

The more you go up, the less messages you receive. If warn is set, you can only receive logs at the level of warn, error, and fatal.

3.2, log level settings

The log level configuration is configured in the configuration file:


logging:
  level:
    root: error

4. Persistent logs

        Why do we need to persist logs, because the above logs are all output on the console, but in the production environment we need to save the logs so that problems can be traced later, the process of saving the logs is called persistence

        To save the log, you only need to specify the log storage directory in the configuration file or specify the log save file name, and SpringBoot will write the console log to the corresponding directory or file~

Configure the save path of the log file:

logging:
  file:
    path: D:\\CCL\\rizhi

 The filename of the configuration file:

logging:
  file:
    path: D:\\CCL\\rizhi\\spring0313.log

 Restart the code, visit the corresponding web page, and then check the corresponding folder:

My repetition is because I accidentally refreshed the page several times just now~


 5. Simpler log output - lombok

Every time you create a new class and need to print log information, you need to:

//1、得到日志对象
    private static Logger logger = LoggerFactory.getLogger(xxxx.class);

It's a bit cumbersome, so use lombok for simpler output:

  1. Add lombok framework support
  2. Use the @slf4j annotation to output logs

5.1. How to add dependencies to the created SpringBoot project 

        If the lombok dependency is not added when the project is first created, you need to add the lombok dependency. Adding dependencies to the already created SpringBoot project requires the help of a plug-in

 Then jump the page to pom.xml, right click:

 

 

 

Then it's ok~ 

5.2. Code changes:

 5.3 Explanation of lombok principle

Let's look at the target directory [target is the final code executed by the project]:

 There is no corresponding slf4j comment, and it becomes the most original operation of creating a log object~

ps: If some students do not have the target directory, do the following:

 Just tick this~

Alright, this is the end of this issue~ See you in the next issue~

Guess you like

Origin blog.csdn.net/LYJbao/article/details/129495111