1. 程式人生 > >logback按日期和大小切分日誌

logback按日期和大小切分日誌

生成的日誌形式為每天一個資料夾並以日期命名,每個資料夾會存放當天的日誌,當每個日誌檔案超過10KB會自動壓縮成.zip


<?xml version="1.0" encoding="UTF-8"?>  
<!-- https://logback.qos.ch/manual/appenders.html 官網 -->
<configuration scan="true">
    <contextName>test</contextName>
    <property name="LOGPATH" value="/data/logs" />
    <!-- 輸出到控制檯 -->
    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>
               [%date{yyyy-MM-dd HH:mm:ss.SSS}] %X{logthreadId} %-5level %logger{80} %method %line - %msg%n
            </pattern>
        </layout>
    </appender>

    <!-- 輸出到檔案 -->
    <appender name="fileLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
   	    <file>${LOGPATH}${file.separator}current.log</file>
        <append>true</append>
        <encoder>
            <pattern>
                [%date{yyyy-MM-dd HH:mm:ss.SSS}] %X{logthreadId} %-5level %logger{80} %method  %line - %msg%n
            </pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOGPATH}${file.separator}%d{yyyy-MM-dd}${file.separator}%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
            <maxHistory>7</maxHistory>
            <maxFileSize>10KB</maxFileSize>
        </rollingPolicy>
    </appender>

    <root level="INFO">
        <appender-ref ref="fileLog" />
        <appender-ref ref="stdout"  />
    </root>
</configuration>