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.
Logs rotate for a reason, so that you only keep so many log files around. In log4j.xml you can add this to you 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 applicatoin 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.
RollingFileAppender does this. You just need to set maxBackupIndex to the highest value for the backup file.
"MaxBackupIndex" works witg RollingFileAppender, but is there any way to do this with DailyRollingFileAppender?
Not working here, I have several log4j properties files for different simultaneous loggers, like this:
log4j.appender.LOGFILE.Threshold=INFO log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout log4j.appender.LOGFILE.layout.ConversionPattern=%d{dd-MM-yyyy} %d{HH:mm:ss} %p %m%n log4j.appender.LOGFILE.MaxFileSize=50000KB log4j.appender.LOGFILE.MaxBackupIndex=2
It used to rotate creating the 2 additional backups, today only the main log file is created and rotates, but no backups, no idea why...
EDIT: I found the problem, it was a permissions issue. The directory where the logs were created had root owner, the Tomcat run under other restricted user and the logs were created by this user. Added 777 to the directory and voila.