Spring Boot系列之-logging

配置文件以application.yml爲例說明:html

Spring Boot默認的日誌組件爲Logback。web

一. 日誌配置參數:spring

logging:
    file:   # 日誌文件,絕對路徑或相對路徑
    path:   # 保存日誌文件目錄路徑
    config: # 日誌配置文件,Spring Boot默認使用classpath路徑下的日誌配置文件,如:logback.xml
    level:  # 日誌級別
        org.springframework.web: DEBUG # 配置spring web日誌級別

 

二. 更改Spring Boot日誌組件爲Log4j(注:Spring Boot僅僅支持Log4j 2.x版本):springboot

<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屬性,以下配置:app

logging:
path: /var/log file: test.log

僅僅只會在項目根路徑下產生test.log文件,不會在指定路徑下產生日誌文件(指望日誌路徑爲:logging.path + logging.file)。
spring-boot

緣由:Spring Boot中的logging.path和logging.file這2個屬性,只須要配置其中之一便可,若是同時配置,則使用logging.file屬性。spa

當配置了loggin.path屬性時,將在該路徑下生成spring.log文件,即:此時使用默認的日誌文件名spring.log日誌

當配置了loggin.file屬性時,將在指定路徑下生成指定名稱的日誌文件。默認爲項目相對路徑,能夠爲logging.file指定絕對路徑。code

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 xml

 

【參考】
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html
http://didispace.com/springbootlog/

相關文章
相關標籤/搜索