问题描述
如何获得log4j删除旧旋转日志文件?我知道我可以设置自动化作业(Windows的cron和unix和计划的任务),但是我希望它可以交叉平台,并且我希望它在应用程序的日志配置中作为我们应用程序的一部分,而不是在OS中的单独代码中特定的脚本语言.我们的应用程序不是用OS脚本语言编写的,我不想在其中进行此部分.
推荐答案
RollingFileAppender这样做.您只需要将maxBackupIndex设置为备份文件的最高值.
其他推荐答案
日志旋转是有原因的,因此您只保留了这么多日志文件.在log4j.xml中,您可以将其添加到您的节点:
<param name="MaxBackupIndex" value="20"/>
该值告诉log4j.xml仅保留20个旋转的日志文件.如果您想要的话,您可以将其限制为5,甚至1.如果您的应用程序没有记录太多数据,并且您有20个日志文件跨越了过去8个月,但是您只需要一个数周的日志,那么我认为您认为您需要调整您的log4j.xml" maxbackupIndex"和" maxfilesize" params.
另外,如果您使用的是属性文件(而不是XML),并希望保存15个文件(例如)
log4j.appender.[appenderName].MaxBackupIndex = 15
其他推荐答案
没有默认值可以控制Dailyrolllingfileappender创建的删除旧日志文件.但是,您可以编写自己的自定义附录器,该应用程序以删除旧日志文件的方式与为RollingFileAppender设置MaxBackupIndex所做的方式几乎相同.
找到的简单说明在这里
来自 1 :
如果您试图将Apache Log4J Dailyrolllingfileappender用于每日日志文件,则可能需要指定应保留的最大文件数量.就像RollingFileAppender支持MaxBackupIndex一样.但是,如果您使用的是dailyrollollingfileappender,那么当前版本的log4j(apache log4j 1.2.16)没有提供任何机制来删除旧日志文件.我尝试在原始版本的dailyrolllingfileappender中进行小修改,以添加maxbackupIndex属性.因此,可以清理旧日志文件,而旧日志文件可能不需要以后使用.
问题描述
How can I get log4j to delete old rotating log files? I know I can set up automated jobs (cron for UNIX and scheduled task for Windows), but I want it cross platform, and I want it in our application's log configuration as a part of our application, rather than in separate code outside in OS specific scripting languages. Our application is not written in OS scripting languages, and I don't want to do this part of it in them.
推荐答案
RollingFileAppender does this. You just need to set maxBackupIndex to the highest value for the backup file.
其他推荐答案
Logs rotate for a reason, so that you only keep so many log files around. In log4j.xml you can add this to your node:
<param name="MaxBackupIndex" value="20"/>
The value tells log4j.xml to only keep 20 rotated log files around. You can limit this to 5 if you want or even 1. If your application isn't logging that much data, and you have 20 log files spanning the last 8 months, but you only need a weeks worth of logs, then I think you need to tweak your log4j.xml "MaxBackupIndex" and "MaxFileSize" params.
Alternatively, if you are using a properties file (instead of the xml) and wish to save 15 files (for example)
log4j.appender.[appenderName].MaxBackupIndex = 15
其他推荐答案
There is no default value to control deleting old log files created by DailyRollingFileAppender. But you can write your own custom Appender that deletes old log files in much the same way as setting maxBackupIndex does for RollingFileAppender.
Simple instructions found here
From 1:
If you are trying to use the Apache Log4J DailyRollingFileAppender for a daily log file, you may need to want to specify the maximum number of files which should be kept. Just like rolling RollingFileAppender supports maxBackupIndex. But the current version of Log4j (Apache log4j 1.2.16) does not provide any mechanism to delete old log files if you are using DailyRollingFileAppender. I tried to make small modifications in the original version of DailyRollingFileAppender to add maxBackupIndex property. So, it would be possible to clean up old log files which may not be required for future usage.