如何将 log4j 配置为仅保留最近 7 天的日志文件? [英] How to configure log4j to only keep log files for the last seven days?

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

问题描述

我有几个使用 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

但出于数据安全原因,我们不允许在我的公司保留日志文件超过 7 天.所以下一个日志文件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 配置为仅保留最近 7 天的日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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