当前位置:   article > 正文

Java服务-常用技术-logback中totalSizeCap和maxHistory无效的解决办法

totalsizecap

最近接入的安全日志项目较多,由于数据量贼大,导致日志输出过多,然后服务器不断报警,经过排查发现logbook中配置的totalSizeCap和maxHistory均没有生效,本来配置的7天,结果发现服务器上日志文件存了60多天,导致服务器磁盘扛不住。特此记录寻找到的解决办法,如下,供同行一起学习交流;

totalSizeCap 无效 升级logbackjar包到1.1.8版本,最好升级到1.2.0版本,因为1.2.0解决了当文件总大小大于2G时不能自动删除旧文件的问题,因为源码中有个文件大小判断是错误的。

maxHistory 无效,需要增加一个设置 <cleanHistoryOnStart>true</cleanHistoryOnStart>,在只有设置为true并且 maxHistory 不为零才能起效。

所以修改后的配置应该是

  1. <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  2. <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
  3. <!-- rollover daily -->
  4. <fileNamePattern>${LOG_HOME}/consumer.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
  5. <!-- each file should be at most 100MB, keep 60 days worth of history, but at most 20GB -->
  6. <maxFileSize>50MB</maxFileSize>
  7. <maxHistory>30</maxHistory>
  8. <totalSizeCap>2GB</totalSizeCap>
  9. <cleanHistoryOnStart>true</cleanHistoryOnStart>
  10. </rollingPolicy>
  11. <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
  12. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n</pattern>
  13. </encoder>
  14. </appender>

 

最后的

  1. <root level="INFO">
  2. <appender-ref ref="FILE" />
  3. </root>

不能少了 ,根据实际配置来,appender name = 后面的参数是啥,就在<appender-ref ref="" /> 写啥

例子:

修改前:

  1. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  2.     <fileNamePattern>${log.home}/ui/${loggingUserId}/mylog_user_%d{yyyy-MM-dd}.log</fileNamePattern>
  3.     <MaxHistory>7</MaxHistory>
  4. </rollingPolicy>

  

修改后(增加【<cleanHistoryOnStart>true</cleanHistoryOnStart>】这一行):

  1. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  2.     <fileNamePattern>${log.home}/ui/${loggingUserId}/mylog_user_%d{yyyy-MM-dd}.log</fileNamePattern>
  3.     <MaxHistory>7</MaxHistory>
  4.     <cleanHistoryOnStart>true</cleanHistoryOnStart>
  5. </rollingPolicy>

以下是两种常见的配置方法

将SizeAndTimeBasedRollingPolicy 改为TimeBasedRollingPolicy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3.     <appender name="File" class="ch.qos.logback.core.rolling.RollingFileAppender">
  4.         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  5.             <fileNamePattern>/home/appadmin/logs/test.%i.log.zip</fileNamePattern>
  6.             <maxHistory>365</maxHistory>
  7.             <totalSizeCap>10GB</totalSizeCap>
  8.             <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
  9.                 <maxFileSize>100 MB</maxFileSize>
  10.             </timeBasedFileNamingAndTriggeringPolicy>
  11.         </rollingPolicy>
  12.     </appender>
  13. </configuration>

将maxFileSize放到timeBasedFileNamingAndTriggeringPolicy 外面

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3. <appender name="File" class="ch.qos.logback.core.rolling.RollingFileAppender">
  4. <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy ">
  5. <fileNamePattern>/home/appadmin/logs/test.%i.log.zip</fileNamePattern>
  6. <maxHistory>365</maxHistory>
  7. <totalSizeCap>10GB</totalSizeCap>
  8. <maxFileSize>100 MB</maxFileSize>
  9. <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
  10. </timeBasedFileNamingAndTriggeringPolicy>
  11. </rollingPolicy>
  12. </appender>
  13. </configuration>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/163270
推荐阅读
相关标签
  

闽ICP备14008679号