Search code examples
javalogginglog4j

How to configure log4j to only keep log files for the last seven days?


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

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?


Solution

  • 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 {} \;