logback日誌的使用,每天生成一個日誌檔案,以及error和其他級別日誌的分離
阿新 • • 發佈:2018-12-29
springboot會自動幫我們讀取logback的配置檔案,我們實現只需要新增即可
在application中配置日誌檔案的位置
logging: config: classpath:conf/logback-dev.xml
配置檔案的內容如下:
<?xml version="1.0" encoding="UTF-8" ?> <configuration> <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern> %d - %msg%n </pattern> </layout> </appender> <!--每天生成日誌的類--> <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"><level>ERROR</level> <!--過濾掉error的級別--> <onMatch>DENY</onMatch> <onMismatch>ACCEPT</onMismatch> </filter> <encoder> <pattern> %msg%n </pattern> </encoder> <!--滾動策略--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--路徑--> <fileNamePattern>H:\ele-web\log/info.%d.log</fileNamePattern> </rollingPolicy> </appender> <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> <!--//列印error--> </filter> <encoder> <pattern> %msg%n </pattern> </encoder> <!--滾動策略--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--路徑--> <fileNamePattern>H:\ele-web\log/error.%d.log</fileNamePattern> </rollingPolicy> </appender> <root level="info"> <appender-ref ref="consoleLog" /> <appender-ref ref="fileInfoLog" /> <appender-ref ref="fileErrorLog" /> </root> </configuration>