Spring Boot系列之-logging
配置文件以 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/