Knowledge about SpringBoot log files

Table of contents

What is the use of logs?

How to use log?

Customized log printing

Get the log object in the program

Print log using log object

Log format

Classification and use of log levels

Log level settings

Log persistence


What is the use of logs?

For us, the main purpose of logs is to troubleshoot and locate problems.

In addition to discovering and locating problems, we can also achieve the following functions through logs:

        Record user login logs to facilitate analysis of whether the user logged in normally or maliciously cracked the user.

        Record the operation log of the system to facilitate data recovery and locate the operator.

        Record the execution time of the program to provide data support for future optimization of the program.

These are all very practical functions provided by logs.

How to use log?

The Spring Boot project has log output by default when it is started, as shown in the following figure:

Through the above log information, we can find the following three problems:

        1. Spring Boot has a built-in logging framework (otherwise, the log will not be output).

        2. By default, the output log is not defined and printed by the developer. So how can the developer customize the printing log in the program?

        3. The log is printed on the console by default, but the console log cannot be saved, so how to save the log permanently?

Customized log printing

Implementation steps for developers to customize print logs:

        1. Get the log object in the program.

        2. Use the relevant syntax of the log object to output the content to be printed. 

Get the log object in the program

To obtain the log object in the program, you need to use the log factory LoggerFactory, as shown in the following code:

private static Logger logger = LoggerFactory.getLogger(UserController.class);

The log factory needs to pass the type of each class in, so that we know the class of the log and can locate the problem class more conveniently and intuitively.

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

Because the logging framework Slf4j is built into Spring Boot, we can directly call slf4j in the program to output logs.

Print log using log object

There are many ways to print log objects. We can first use the info() method to output the log, as shown in the following code:

logger.info("--------------要输出日志的内容----------------");

Log format

Classification and use of log levels

trace: trace, meaning a little, the lowest level;

debug: Print key information when debugging is needed;

info: ordinary printing information (default log level);

warn: Warning, does not affect use, but requires attention;

error: error information, higher-level error log information;

fatal: Fatal, an event that causes the program to exit execution due to a code exception.

Log level order:

The higher the level, the fewer messages are received. If warn is set, only warning, error, and fatal level logs will be received.

Log level settings

Log level configuration only requires setting the "logging.level" configuration item in the configuration file, as shown below:

logging:
  level:
   root: error

Configure the log level of the path.

@RestController
@RequestMapping("/user")
public class UserController {
     // 1.得到⽇志对象
     private static Logger logger =
             LoggerFactory.getLogger(UserController.class);
     @Value("${server.port}")
     private String port;
     @Value("${spring.datasource.url}")
     private String url;
     @RequestMapping("/sayhi")
     public String sayHi() {
     // 2.使⽤⽇志打印⽇志
     logger.trace("================= trace ===============");
     logger.debug("================= debug ===============");
     logger.info("================= info ===============");
     logger.warn("================= warn ===============");
     logger.error("================= error ===============");
     return "Hi," + url;
     }
}

The default log output level is info

Log persistence

The above logs are all output on the console. However, in a production environment, we need to save the logs so that we can trace the problems after problems occur. The process of saving the logs is called persistence.

If you want to persist the log, you only need to specify the log storage directory in the configuration file or specify the log saving file name, and Spring Boot will write the console log to the corresponding directory or file.

Configure the save path of the log file:

# 设置⽇志⽂件的⽬录
logging:
 file:
 path: D:\\home\\test

Configure the file name of the log file:

# 设置⽇志⽂件的⽬录
logging:
 file:
 path: D:/home/tests/pring-1204.log

Guess you like

Origin blog.csdn.net/m0_62468521/article/details/131360188