日志体系
Spring Boot 使用commons-logging作为日志接口,目前支持Log4J2、Logback和JUL( Java Util Logging) 三种实现方式,如下图所示:
spring-boot-starter-logging
作用: 使用Logback日志框架;
实现原理:
- 如果存在slf4j接口,则将其桥接到commons-logging;
- 使用Logback作为日志实现框架;
spring-boot-starter-log4j
作用: 使用Log4J日志框架;
实现原理:
- 如果存在slf4j接口,则将其桥接到commons-logging;
- 使用Log4J作为日志实现框架;
spring-boot-starter-log4j2
作用: 使用Log4J2日志框架;
使用配置: 需要将默认的logback排除,同时引入log4j2,如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
日志的定制
spring-boot相关日志配置
参考:
- Spring Boot官网:https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/#boot-features-logging;
- slf4j官网:https://www.slf4j.org/legacy.html;
- https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#howto-configure-log4j-for-logging;
- SpringBoot 默认属性列表:https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html#common-application-properties;