官方文档:
通过看文档整理以下几点:
1.Spring Boot 通过引入 spring-boot-starter-web 模块即可引入日志相关依赖包。
2.Spring Boot 根据类路径下的相关日志框架的依赖包去自动构建日志系统。首选日志框架是 Logback。
83.1 Configure Logback for Logging
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework.web" level="DEBUG"/>
</configuration>
83.1.1 Configure Logback for File-only Output
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
Demo
在 src/main/resource 下创建文件 logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE" value="d:/log/my.log"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
<logger name="org.springframework.web" level="INFO"/>
</configuration>
相关技术点
1.可以设置不同功能模块的日志级别
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
2.可以更改日志配置文件的存储位置
logging.config=classpath:logback-spring.xml
3.源码切入点
LoggingApplicationListener