How to configure logging in Spring Boot?
In Spring Boot, you can configure log output using a configuration file. Below is an example configuration file that allows you to output logs to both the console and a file:
application.properties file:
# 设置日志输出级别
logging.level.root=info
# 控制台输出配置
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} %clr(%5p) %clr([%15.15t]) %-40.40logger{39} : %m%n
# 文件输出配置
logging.file=logs/application.log
logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
In the above example, the log output level is configured as info, meaning that only log information at the info level and above will be output.
In the console output configuration, %clr is used to set colors, %d represents date and time, %p represents log level, %t represents thread name, %logger{39} represents class name, %m represents log content, and %n represents a new line.
In the file output configuration, %thread is used to represent the thread name, %level for the log level, %logger{36} for the class name, %msg for the log content, and %n for a new line. The log file will be output to logs/application.log.
The above configuration can be modified according to actual needs. The configuration file can be either application.properties or application.yml, placed in the src/main/resources directory of the project to take effect.