logback配置僅根據文件大小進行滾動


1、logback的日志策略

1)ch.qos.logback.core.rolling.TimeBasedRollingPolicy

按照時間滾動,例如按天或按月。

2)ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy
按日期歸檔文件,但同時限制每個日志文件的大小(時間與大小)

3)ch.qos.logback.core.rolling.FixedWindowRollingPolicy

滾動時,根據固定窗口算法重命名文件,可以設置最大(maxIndex)或最小(minIndex)的窗口索引,搭配 < triggeringPolicy >標簽可以實現根據文件大小進行滾動的需求。

2、實例

注:file 屬性必須設置,不然會報錯 The File name property must be set before using this rolling policy...

<configuration>
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>/data/logs/test.log</file>

    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
      <fileNamePattern>/data/logs/test_%i.log</fileNamePattern>
      <minIndex>1</minIndex>
      <maxIndex>3</maxIndex>
    </rollingPolicy>

    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
      <maxFileSize>100MB</maxFileSize>
    </triggeringPolicy>
      
    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender>
        
  <root level="DEBUG">
    <appender-ref ref="FILE" />
  </root>
</configuration>

/data/logs/test.log 是日志名

fileNamePattern中的 %i 是窗口索引,minIndex與maxIndex是窗口數量的最小值以及最大值

maxFileSize 就是文件大小滾動的條件

3、實現結果

-rw-r--r-- 1 root root 3233341 8月  23 09:33 test.log

到達100M會根據序號遞增 test_1.log -> test_2.log -> test_3.log


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM