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