LogCleanerJob

This job implementation infers a logfile's age by its filename and deletes files older than a given age. Ample details are in the javadocs of the class.

Author
Mirko Caserta
Quartz Interface
org.quartz.Job
Quartz Tested Version(s)
1.6
Source Code
LogCleanerJob.java

Usage Example

Suppose you have a log directory with an absolute path of: /home/bea/user_projects/domains/mydomain/logs

The log files are named so that they end with the date the last time the log file was accessed. An example file name could be: myserver.log.2005-12-02

This example runs every morning at 9:30 and deletes files older than 60 days:

JobDetail jobDetail = new JobDetail("GrimReaper", null, LogCleanerJob.class);
JobDataMap jobDataMap = jobDetail.getJobDataMap();
jobDataMap.put(LogCleanerJob.DATAMAP_KEY_LOG_DIR, "/home/bea/user_projects/domains/mydomain/logs");
jobDataMap.put(LogCleanerJob.DATAMAP_KEY_DELETE_IF_AGE_GREATER_THAN_DAYS, new Integer(60));
jobDataMap.put(LogCleanerJob.DATAMAP_KEY_DATE_IN_FILENAME_REGEX, "^.+\\.log\\.(\\d{4}-\\d{2}-\\d{2})$");
jobDataMap.put(LogCleanerJob.DATAMAP_KEY_FILENAME_SIMPLE_DATE_FORMAT_PATTERN, "yyyy-MM-dd");
jobDataMap.put(LogCleanerJob.DATAMAP_KEY_DELETE_EMPTY_DIRS, Boolean.FALSE);

scheduler.scheduleJob(jobDetail, TriggerUtils.makeDailyTrigger("DailyGrimReaper", 9, 30));