如何配置log4j只保留过去七天的日志文件? [英] How to configure log4j to only keep log files for the last seven days?

查看:1822
本文介绍了如何配置log4j只保留过去七天的日志文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于使用 log4j 进行日志记录的几个Java应用程序,我遇到以下日志记录问题:

I have the following logging problem with several Java applications using log4j for logging:

我想要日志文件每天轮换,例如

I want log files to be rotated daily, like

log.2010-09-10
log.2010-09-09
log.2010-09-08
log.2010-09-07
log.2010-09-06
log.2010-09-05
log.2010-09-04

但出于数据安全原因,我们不允许将日志文件保留超过七天在我的公司。因此,生成下一个下一个日志文件 log.2010-09-11 应该会触发删除 log.2010-09-04 。是否可以使用 log4j 配置此类行为?如果没有,你知道这种日志问题的另一个优雅解决方案吗?

But for data security reasons we are not allowed to keep log files for longer than seven days at my company. So the generation of the next next log file log.2010-09-11 should trigger the deletion of log.2010-09-04. Is it possible to configure such a behaviour with log4j? If not, do you know another elegant solution for this kind of logging problem?

推荐答案

你可以在一个单独的脚本中执行你的内务管理可以每天运行。这样的事情:

You can perform your housekeeping in a separate script which can be cronned to run daily. Something like this:

find /path/to/logs -type f -mtime +7 -exec rm -f {} \;

这篇关于如何配置log4j只保留过去七天的日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆