配置文件以application.yml為例說明:
Spring Boot默認的日志組件為Logback。
一. 日志配置參數:
logging:
file: # 日志文件,絕對路徑或相對路徑
path: # 保存日志文件目錄路徑
config: # 日志配置文件,Spring Boot默認使用classpath路徑下的日志配置文件,如:logback.xml
level: # 日志級別
org.springframework.web: DEBUG # 配置spring web日志級別
二. 更改Spring Boot日志組件為Log4j(注:Spring Boot僅僅支持Log4j 2.x版本):
<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日志文件路徑的疑惑?
同時配置了logging.path和logging.file屬性,如下配置:
logging:
path: /var/log
file: test.log
僅僅只會在項目根路徑下產生test.log文件,不會在指定路徑下產生日志文件(期望日志路徑為:logging.path + logging.file)。
原因:Spring Boot中的logging.path和logging.file這2個屬性,只需要配置其中之一即可,如果同時配置,則使用logging.file屬性。
當配置了loggin.path屬性時,將在該路徑下生成spring.log文件,即:此時使用默認的日志文件名spring.log
當配置了loggin.file屬性時,將在指定路徑下生成指定名稱的日志文件。默認為項目相對路徑,可以為logging.file指定絕對路徑。
logging:
path: /var/logs # 在/var/logs目錄下生成spring.log文件
file: /var/logs/test.log # 在/var/logs目錄下生成test.log文件
詳見:http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html
【參考】
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html
http://didispace.com/springbootlog/