logback踩坑记录

大老板 1年前 ⋅ 1548 阅读

坑1-启动未删除之前的历史日志

logback测试版本1.2.3

<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
      <file>${LOG_PATH}/info.log</file>
      <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
          <fileNamePattern>${LOG_PATH}/history/info.%d{yyyy-MM-dd}.%i.${COMPRESS_SUFFIX}</fileNamePattern>
          <maxHistory>5</maxHistory>
          <totalSizeCap>1GB</totalSizeCap>
          <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
              <!--文件达到 最大10MB时会被压缩和切割 -->
              <maxFileSize>100MB</maxFileSize>
          </timeBasedFileNamingAndTriggeringPolicy>
      </rollingPolicy>
      <encoder charset="UTF-8">
          <pattern>${PATTERN}</pattern>
      </encoder>
  </appender>

该配置下,应用每次启动都不会删除历史日志文件 需要增加下面这个配置 <cleanHistoryOnStart>true</cleanHistoryOnStart>

坑2-启动删除历史日志,但未按maxHistory设置删除maxHistory之前的日志

logback测试版本1.2.3

<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
      <file>${LOG_PATH}/info.log</file>
      <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
          <fileNamePattern>${LOG_PATH}/history/info.%d{yyyy-MM-dd}.%i.${COMPRESS_SUFFIX}</fileNamePattern>
          <maxHistory>5</maxHistory>
          <totalSizeCap>1GB</totalSizeCap>
          <cleanHistoryOnStart>true</cleanHistoryOnStart>
          <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
              <!--文件达到 最大10MB时会被压缩和切割 -->
              <maxFileSize>100MB</maxFileSize>
          </timeBasedFileNamingAndTriggeringPolicy>
      </rollingPolicy>
      <encoder charset="UTF-8">
          <pattern>${PATTERN}</pattern>
      </encoder>
  </appender>

启动后只会删除5-37天之间的日志,更早的不会删除


全部评论: 0

    我有话说: