0920-logback-spring.xml自定義按時間分割日誌,指定某個類輸出到某個檔案
阿新 • • 發佈:2019-01-22
廢話不多說,直接看程式碼,裡面有註釋很好理解
目錄結構
<?xml version="1.0" encoding="utf-8" ?> <configuration xmlns="http://ch.qos.logback/xml/ns/logback" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ch.qos.logback/xml/ns/logback https://raw.githubusercontent.com/enricopulatzo/logback-XSD/master/src/main/xsd/logback.xsd"> <!--定義日誌檔案的儲存地址 勿在 LogBack 的配置中使用相對路徑--> <property name="log-home" value="./logs"/> <property name="std-format" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}: %msg%n"/> <property name="ad-format" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}: %msg%n"/> <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender" > <!-- 輸出的格式 --> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>${std-format}</pattern> </encoder> </appender> <appender name="fileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${log-home}/gateway.log</file> <!-- 配置滾動的策略 --> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 日誌名稱的格式 --> <fileNamePattern>${log-home}/gateway.%d{yyyy-MM-dd}.log</fileNamePattern> <!-- 儲存的最長時間:天數 --> <MaxHistory>7</MaxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>${std-format}</pattern> </encoder> </appender> <!--處理ADService層日誌--> <appender name="adFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${log-home}/ad.log</file> <!-- 配置滾動的策略 --> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 日誌名稱的格式 --> <fileNamePattern>${log-home}/ad.%d{yyyy-MM-dd}.log</fileNamePattern> <!-- 儲存的最長時間:天數 --> <MaxHistory>7</MaxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>${ad-format}</pattern> </encoder> </appender> <appender name="adConsoleAppender" class="ch.qos.logback.core.ConsoleAppender" > <!-- 輸出的格式 --> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>${ad-format}</pattern> </encoder> </appender> <!--ADService層日誌--> <!--<logger name="com.gatewayserver.gatewayserver.service.impl.AdServiceImpl" level="INFO" additivity="false">--> <!--<appender-ref ref="adConsoleAppender" />--> <!--<appender-ref ref="fileAppender"/>--> <!--</logger>--> <root level="info"> <appender-ref ref="consoleAppender"/> <appender-ref ref="fileAppender"/> </root> </configuration>
輸出結果
額外的,幫助大家提供一個好用的logback的xmlns(官方不提供),方便寫xml
<?xml version="1.0" encoding="utf-8" ?> <configuration xmlns="http://ch.qos.logback/xml/ns/logback" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ch.qos.logback/xml/ns/logback https://raw.githubusercontent.com/enricopulatzo/logback-XSD/master/src/main/xsd/logback.xsd"> </configuration>