删除N天前的log日志文件:RollingFileAppender,DailyRollingFileAppender,/etc/cron
2017-02-13 21:26 Loull 阅读(2121) 评论(0) 收藏 举报1. 如果您使用的是Log4j,且采用的RollingFileAppender方式, 通过设置maxBackupIndex属性来指定要保留的日志文件数的最大值可以间接实现删除N天前的日志文件。
2. 如果您使用的是Log4j,且采用的DailyRollingFileAppender方式,由于该方式不支持maxBackupIndex,需要重新实现DailyRollingFileAppender,用以支持maxBackupIndex的设置。具体请参考http://macrochen.iteye.com/blog/1346993
3. 如果您使用的是logback,可以通过设置maxHistory实现删除N天前的日志。
4. 可以通过Linux的cron job实现定期删除文件,具体如下
# cd /etc/cron.daily # vi logcron 输入如下内容 #!/bin/sh find /logs -type f -ctime +30 | xargs rm -rf (这里实现了删除30天之前文件的命令) 然后保存该文件,最后执行如下命令给该文件服务可执行权限 # chmod +x /etc/cron.daily/logcron
配置/ect/cron.daily 执行时间:
$ cat /etc/anacrontab # /etc/anacrontab: configuration file for anacron # See anacron(8) and anacrontab(5) for details. SHELL=/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # the maximal random delay added to the base delay of the jobs RANDOM_DELAY=45 # the jobs will be started during the following hours only START_HOURS_RANGE=3-22 #period in days delay in minutes job-identifier command 1 5 cron.daily nice run-parts /etc/cron.daily 7 25 cron.weekly nice run-parts /etc/cron.weekly @monthly 45 cron.monthly nice run-parts /etc/cron.monthly
RollingFileAppender
maxBackupIndex
There is one backup file by default.
maxFileSize
The default maximum file size is 10MB.
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/RollingFileAppender.html
自定义DailyRollingFileAppender:
Log4j中DailyRollingFileAppender日志文件清理策略,http://blog.csdn.net/pl1612127/article/details/50504592
Log4j的扩展-支持设置最大日志数量的DailyRollingFileAppender, http://www.cnblogs.com/rembau/p/5201001.html
https://www.iteblog.com/archives/1634.html
扩展:
Linux日志文件总管——logrotate : https://linux.cn/article-4126-1.html
浙公网安备 33010602011771号