logback 每天生成和大小生成 冲突 TimeBasedRollingPolicy SizeBasedTriggeringPolicy

项目使用了logback,日志打印需要按照每天和大小生成日志,于是使用了TimeBasedRollingPolicy SizeBasedTriggeringPolicy

  1. <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  2. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  3. <FileNamePattern>${LOG_HOME}/consumer.%d{yyyy-MM-dd_HH}.log FileNamePattern>
  4. <MaxHistory>30 MaxHistory>
  5. rollingPolicy>
  6. <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
  7. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n pattern>
  8. encoder>
  9. <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
  10. <MaxFileSize>10MB MaxFileSize>
  11. triggeringPolicy>
  12. appender>
但是在实际使用中并未有按照预期根据日期和大小生成日志,查了了相关文章说是TimeBasedRollingPolicy和SizeBasedTriggeringPolicy冲突,在根据网上的资料去看了logback的官网地址 https://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedFNATP,在游览了之后发现了最重要的部分
  1. Size and time based rolling policy
  2. Sometimes you may wish to archive files essentially by date but at the same time limit the size of each log file, in particular if post-processing tools impose size limits on the log files. In order to address this requirement, logback ships with SizeAndTimeBasedRollingPolicy.
  3. Note that TimeBasedRollingPolicy already allows limiting the combined size of archived log files. If you only wish to limit the combined size of log archives, then TimeBasedRollingPolicy described above and setting the totalSizeCap property should be amply sufficent.
  4. Here is a sample configuration file demonstrating time and size based log file archiving.
  5. Example: Sample configuration for SizeAndTimeBasedFNATP (logback-examples/src/main/resources/chapters/appenders/conf/logback-sizeAndTime.xml)
  6. View as .groovy
  7. <configuration>
  8. <appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
  9. <file>mylog.txt file>
  10. <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
  11. <fileNamePattern>mylog-%d{yyyy-MM-dd}.%i.txt fileNamePattern>
  12. <maxFileSize>100MB maxFileSize>
  13. <maxHistory>60 maxHistory>
  14. <totalSizeCap>20GB totalSizeCap>
  15. rollingPolicy>
  16. <encoder>
  17. <pattern>%msg%n pattern>
  18. encoder>
  19. appender>
  20. <root level="DEBUG">
  21. <appender-ref ref="ROLLING" />
  22. root>
  23. configuration>
然后就发现logback还有一个SizeAndTimeBasedRollingPolicy,所以根据官网修改我的配置文件为
  1. <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  2. <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
  3. <fileNamePattern>${LOG_HOME}/consumer.%d{yyyy-MM-dd}.%i.log fileNamePattern>
  4. <maxFileSize>50MB maxFileSize>
  5. <maxHistory>30 maxHistory>
  6. rollingPolicy>
  7. <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
  8. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n pattern>
  9. encoder>
  10. appender>
然后就ok了,日志文件中的%d、 %i是不可缺少的。

你可能感兴趣的:(logback)