SpringBoot--logback-spring.xml 配置
阿新 • • 發佈:2018-12-16
方便日後複習和供大家使用,將springboot 專案下的 classpath:logback-spring.xml 如下:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <logger name="org.springframework.web" level="DEBUG"/> <!-- 檔案輸出格式 --> <property name="PATTERN" value="%-12(%d{yyyy-MM-dd HH:mm:ss.SSS}) |-%-5level [%thread] %c [%L] -| %msg%n" /> <!-- test檔案路徑 --> <property name="TEST_FILE_PATH" value="E:\lo4j2\logs" /> <!-- pro檔案路徑 --> <property name="PRO_FILE_PATH" value="E:\lo4j2\logs" /> <!-- 開發環境 --> <springProfile name="dev"> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${PATTERN}</pattern> </encoder> </appender> <logger name="com.nyist.springdemo41" level="debug"/> <root level="info"> <appender-ref ref="CONSOLE" /> </root> </springProfile> <!-- 測試環境 --> <springProfile name="test"> <!-- 每天產生一個檔案 --> <appender name="TEST-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- 檔案路徑 --> <file>${TEST_FILE_PATH}</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 檔名稱 --> <fileNamePattern>${TEST_FILE_PATH}/info.%d{yyyy-MM-dd}.log</fileNamePattern> <!-- 檔案最大儲存歷史數量 --> <MaxHistory>100</MaxHistory> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>${PATTERN}</pattern> </layout> </appender> <root level="info"> <appender-ref ref="TEST-FILE" /> </root> </springProfile> <!-- 生產環境 --> <springProfile name="prod"> <appender name="PROD_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${PRO_FILE_PATH}</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${PRO_FILE_PATH}/warn.%d{yyyy-MM-dd}.log</fileNamePattern> <MaxHistory>100</MaxHistory> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>${PATTERN}</pattern> </layout> </appender> <root level="warn"> <appender-ref ref="PROD_FILE" /> </root> </springProfile> </configuration>
一、以上是logback-spring.xml,spring boot預設會載入classpath:logback-spring.xml或者classpath:logback-spring.groovy
使用自定義配置檔案,配置方式為:
logging.config=classpath:logback-nyist.xml(在application.properties中配置)
注意:不要使用logback這個來命名,否則spring boot將不能完全例項化
二、程式碼中的專案名稱和logs日誌檔案路徑需要改變