Article directory
- foreword
-
- How to use the log
- Analysis log
- Custom print log
- Common log configuration operations
- Log Level - Understand
- Why SpringBoot can print logs and set log levels
- Set the log level for the current project, and set the level for a single folder separately
- Log persistence - the following methods can be stored
- Lombok's pre-work
- Methods provided by Lombok
- The realization principle of Lombok
foreword
The above describes the construction of the SpringBoot project and the use of configuration files. Next, we will introduce the log files of SpringBoot. The log plays a very important role in the program. Whoever writes the program can not report errors. The log is a way to let You quickly find the wrong way! , of course, the log is not only for finding errors, but also has many uses, such as logging in logs, recording who logs in, etc., but for us programmers, the most important thing is to locate errors
How to use the log
The Spring Boot project has log output by default when it starts, as shown in the following figure:
These logs are default. SpringBoot has a built-in log framework. Generally, we print custom logs, and the logs are printed on the console by default and cannot be saved, so we have to learn how to save the logs permanently.
Analysis log
The last one is: log content
Use System.out, println to print logs
1: No time (you can add it yourself)
2: No log level
3: No execution position of printed logs (you can add it)
4: Unable to persist logs (most serious)
Custom print log
① : Get the log object in the program. To get the log in the program, you need a class, LoggerFactory, as shown in the following code
private static Logger logger = LoggerFactory.getLogger(LogController.class);
Let me talk about it here: getLogger contains the class object to be printed, and it cannot be written indiscriminately. It provides the path to find the output log and save the log
package com.example.demo.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
public class LogController {
private static Logger logger = LoggerFactory.getLogger(LogController.class);
@RequestMapping("/log")
public void log(){
// 使用logger打印对象
logger.info("这是测试打印日志的一条语句");
}
}
The log factory needs to pass in the type of each class, so that we can know the class of the log and locate the problem class more conveniently and intuitively. The Logger object belongs to the org.slf4j package. Do not import The log framework Slf4j is built into the error package
Spring Boot, so we can directly call slf4j in the program to output logs
Print log - print effect
logger.info("--------------要输出⽇志的内容----------------");
Common log configuration operations
The file name of the configuration log
logging:
file:
name: D:\\home\\ruoyi\\spring-1204.log
Configure the save path of the log
# 设置⽇志⽂件的⽬录
logging:
file:
path: D:\\home\\ruoyi
Configure the log level
logging:
level:
root: error
配置不同包下面的级别 ,
logging.level.com.example.demo.test=trace
Log Level - Understand
trace: trace, meaning 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, high-level error log information;
fatal: Fatal, the event that the program exits because of a code exception.
To configure the log level, you only need to set the "logging.level" configuration item in the configuration file, as shown below:
logging:
level:
root: error
Why SpringBoot can print logs and set log levels
Because SpringBoot has two built-in log frameworks, SLF4J+LogBack,
the framework LogBack that SLF4J allows developers to use and call
is the lowest-level framework for implementing log-related operations
Note: The default log level of SpringBoot is Info
Set the log level for the current project, and set the level for a single folder separately
Log persistence - the following methods can be stored
The above logs are all output on the console. If you want to save the logs permanently, you need to specify the log storage directory in the configuration file or specify the log save file name, and Spring Boot will The log of the console will be written to the corresponding directory or file.
The log file is set by the default size, if it exceeds a file will be restarted
The log is added in an appended way
# 设置⽇志⽂件的⽬录
logging.file.path= D:\\home\\ruoyi
The output result is as follows
# 设置⽇志⽂件的⽬录
logging:
file:
path: D:\\home\\ruoyi
# 设置⽇志⽂件的⽂件名
logging:
file:
name: D:\\home\\ruoyi\\spring-1204.log
Of course there is an easier way, use the artifact L:ombok to solve this log problem
Lombok's pre-work
① Regardless of the version of IDEA, the plug-in must be installed
②,Add Lombok dependency
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
Then add the @Slf4j annotation to the class, and finally print the log with the log object. Using
@Slf4j is equivalent to private static Logger logger =LoggerFactory.getLogger(LogController.class); this sentence
Methods provided by Lombok
@Getter automatically add getter method
@Setter automatically add setter method
@ToString automatically add toString method
@EqualsAndHashCode automatically add equals and hashCode method
@NoArgsConstructor automatically add no-argument construction method
@AllArgsConstructor automatically add all Attribute construction method, the order is according to the definition order of the attribute
@NonNull attribute cannot be null
@RequiredArgsConstructor automatically adds the construction method of the required attribute,
the attribute of final + @NonNull is required
The @Data annotation provides most of the above methods, except @NoArgsConstructor automatically adds no-argument constructors
@AllArgsConstructor automatically adds all-attribute constructors, and the order follows the definition order of the attributes
@NonNull attributes cannot be null These three .the rest
The realization principle of Lombok
Lombok converts the code during compilation, which will not affect the efficiency during runtime
. It is equivalent to generating corresponding methods for you during compilation.