SpringBoot advanced - log level configuration and operation
1. Log level
- trace: lowest level
- debug: for debugging, usually used to track program progress
- info: For recording, usually used to record program behavior
- warn: warning
- error: error
- fatal: catastrophic error, highest level
2. Set the log level
The default log level of SpringBoot is info, which means that only info or higher level logs are output on the console
If you want to temporarily change the log level for debugging during the development phase, you can use the following simple configuration:
debug: true
To really set the project log level, you need to do this:
logging:
level:
root: info
com.mzz.example.controller: debug
com.mzz.example.service: debug
The level of the log is set according to the group or package, where the root group is the entire project
But it is cumbersome to set the log level in units of packages, so you can set the group yourself, and then set the level for each group separately:
logging:
group:
server: com.mzz.example.service, com.mzz.example.controller
sql: com.mzz.example.mapper
level:
root: info
server: debug
sql: trace
3. Print the log
To print the log, you must first obtain the log object, as follows:
@RestController
@RequestMapping("/user")
public class UserController {
public static final Logger log = LoggerFactory.getLogger(BookController.class);
}
Since this line of code is relatively fixed, it can be replaced by the @Slf4j annotation in Lombok, as follows:
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
}
After getting the log object, call the method to print the log, and use each level as the method name to print the log of the corresponding level:
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@PostMapping
public Result save(@RequestBody User user) {
log.debug("尝试保存新用户:{}", user);
boolean flag = bookService.save(user);
if (!flag) {
log.error("保存新用户失败:{}", user);
}
log.info("已保存新用户:{}", user);
return Result.success();
}
}
When printing logs, {} can be used as a placeholder, and the array is passed in later
In addition, there is no fatal-level log printing method, because fatal refers to a serious error that will cause the program to crash, and the program crash log system cannot continue to run, so printing fatal-level logs is meaningless
4. Custom log format
Set the logging.pattern.console property to customize the format of the console print log, as follows:
logging:
pattern:
console: "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n"
The above is the default log format of SpringBoot, refer to: https://blog.csdn.net/qq_34049732/article/details/116722116
5. Documentation log
See the following example:
logging:
file:
name: log/server.log # 指定文件名称以及路径:启动路径下的 log 文件夹下
# path: log/ # 指定日志文件路径,不含名称,默认为 spring.log
logback:
rollingpolicy:
max-file-size: 1MB # 指定文件最大规格
file-name-pattern: log/server_%d{
yyyy-MM-dd}_%i.log # 指定文件名称格式以及路径
Rolling logs can set the maximum size of each log
Setting the rolling log still needs to set the log name, such as server.log above, which contains the latest log
You only need to configure one logging.file.name and logging.file.path, if they exist at the same time, the path will be invalid