logback 通用配置方案 顏色和結合application.yml
阿新 • • 發佈:2019-01-09
通用配置方案 顏色和結合application.yml。
1.包含顏色
2.在yml中指定包的日誌等級
3.指定日誌的根目錄
4.指定日誌檔案的名字
5.錯誤日誌作了單獨的二次儲存
6.按檔案大小和日期滾動
7.日誌目錄格式如下
/logs
name-log-all.log
name-log-error.log
/backup
name-log-all-2017-09-24.0.log
application.yml
logging: home: "logs" name: "sell" level: root: "info" org.springframework: "info" com.gong: "debug"
logback-spring.xml
<?xml version="1.0" encoding="utf-8" ?> <configuration> <springProperty scope="context" name="logHome" source="logging.home" defaultValue="logs"/> <property name="LOG_HOME" value="${logHome}"></property> <springProperty scope="context" name="logName" source="logging.name" defaultValue="app"/> <property name="LOG_NAME" value="${logName}"></property> <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" /> <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" /> <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" /> <property name="FILE_LOG_PATTERN" value="${FILE_LOG_PATTERN:-%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/> <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/> <appender name="consoleLog" class = "ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern> ${CONSOLE_LOG_PATTERN} </pattern> </layout> </appender> <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!--<filter class="ch.qos.logback.classic.filter.LevelFilter">--> <!--<level>ERROR</level>--> <!--<onMatch>DENY</onMatch>--> <!--<onMismatch>ACCEPT</onMismatch>--> <!--</filter>--> <encoder> <pattern> ${FILE_LOG_PATTERN} </pattern> </encoder> <file>${LOG_HOME}/${LOG_NAME}-all.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern> <!--%d 日期格式 %i 指定Rolling時的序號--> ${LOG_HOME}/backup/${LOG_NAME}-log-all-%d{yyyy-MM-dd}.%i.log </fileNamePattern> <maxHistory>365</maxHistory> <!--指定回滾大小--> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>5MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> </appender> <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> </filter> <encoder> <pattern> ${FILE_LOG_PATTERN} </pattern> </encoder> <file>${LOG_HOME}/${LOG_NAME}-error.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern> ${LOG_HOME}/backup/${LOG_NAME}-log-error-%d{yyyy-MM-dd}.%i.log </fileNamePattern> <maxHistory>365</maxHistory> <!--指定回滾大小--> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>5MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> </appender> <root level="${logging.level.root}"> <appender-ref ref="consoleLog"/> <appender-ref ref="fileInfoLog"/> <appender-ref ref="fileErrorLog"/> </root> </configuration>
結果如圖