赞
踩
最近接入的安全日志项目较多,由于数据量贼大,导致日志输出过多,然后服务器不断报警,经过排查发现logbook中配置的totalSizeCap和maxHistory均没有生效,本来配置的7天,结果发现服务器上日志文件存了60多天,导致服务器磁盘扛不住。特此记录寻找到的解决办法,如下,供同行一起学习交流;
totalSizeCap 无效 升级logbackjar包到1.1.8版本,最好升级到1.2.0版本,因为1.2.0解决了当文件总大小大于2G时不能自动删除旧文件的问题,因为源码中有个文件大小判断是错误的。
maxHistory 无效,需要增加一个设置 <cleanHistoryOnStart>true</cleanHistoryOnStart>,在只有设置为true并且 maxHistory 不为零才能起效。
所以修改后的配置应该是
- <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
-
-
-
- <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
-
- <!-- rollover daily -->
-
- <fileNamePattern>${LOG_HOME}/consumer.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
-
- <!-- each file should be at most 100MB, keep 60 days worth of history, but at most 20GB -->
- <maxFileSize>50MB</maxFileSize>
- <maxHistory>30</maxHistory>
- <totalSizeCap>2GB</totalSizeCap>
- <cleanHistoryOnStart>true</cleanHistoryOnStart>
- </rollingPolicy>
-
- <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
-
- <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n</pattern>
-
- </encoder>
-
- </appender>
最后的
- <root level="INFO">
-
- <appender-ref ref="FILE" />
-
- </root>
不能少了 ,根据实际配置来,appender name = 后面的参数是啥,就在<appender-ref ref="" /> 写啥
例子:
修改前:
- <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
-
- <fileNamePattern>${log.home}/ui/${loggingUserId}/mylog_user_%d{yyyy-MM-dd}.log</fileNamePattern>
-
- <MaxHistory>7</MaxHistory>
-
- </rollingPolicy>
修改后(增加【<cleanHistoryOnStart>true</cleanHistoryOnStart>】这一行):
- <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
-
- <fileNamePattern>${log.home}/ui/${loggingUserId}/mylog_user_%d{yyyy-MM-dd}.log</fileNamePattern>
-
- <MaxHistory>7</MaxHistory>
-
- <cleanHistoryOnStart>true</cleanHistoryOnStart>
-
- </rollingPolicy>
以下是两种常见的配置方法
将SizeAndTimeBasedRollingPolicy 改为TimeBasedRollingPolicy
- <?xml version="1.0" encoding="UTF-8"?>
- <configuration>
- <appender name="File" class="ch.qos.logback.core.rolling.RollingFileAppender">
- <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
- <fileNamePattern>/home/appadmin/logs/test.%i.log.zip</fileNamePattern>
- <maxHistory>365</maxHistory>
- <totalSizeCap>10GB</totalSizeCap>
- <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
- <maxFileSize>100 MB</maxFileSize>
- </timeBasedFileNamingAndTriggeringPolicy>
- </rollingPolicy>
- </appender>
- </configuration>
将maxFileSize放到timeBasedFileNamingAndTriggeringPolicy 外面
- <?xml version="1.0" encoding="UTF-8"?>
- <configuration>
- <appender name="File" class="ch.qos.logback.core.rolling.RollingFileAppender">
- <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy ">
- <fileNamePattern>/home/appadmin/logs/test.%i.log.zip</fileNamePattern>
- <maxHistory>365</maxHistory>
- <totalSizeCap>10GB</totalSizeCap>
- <maxFileSize>100 MB</maxFileSize>
- <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
- </timeBasedFileNamingAndTriggeringPolicy>
- </rollingPolicy>
- </appender>
- </configuration>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。