--- org/AccessLogValve.java Wed Mar 28 12:59:23 2012 +++ new/AccessLogValve.java Wed Mar 28 13:04:30 2012 @@ -20,6 +20,7 @@ import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; +import java.io.FilenameFilter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; @@ -28,6 +29,8 @@ import java.nio.charset.Charset; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; import java.util.Date; import java.util.Enumeration; import java.util.HashMap; @@ -271,8 +274,16 @@ * is true. */ protected File currentLogFile = null; - + /** + * Whether or not to delete older files + */ + private boolean deleteArchiveFiles = false; /** + * The maximum number of old files to keep around. Must be greater than 1 + */ + private int archiveFileCount = 14; //Arbitrarily choose 2 weeks when using the default fileDateFormat setup + + /** *

Cache structure for formatted timestamps based on seconds.

* *

The cache consists of entries for a consecutive range of @@ -990,9 +1001,51 @@ } + public void setArchiveFileCount(int count) { + archiveFileCount = count; + } + public int getArchiveFileCount() { + return archiveFileCount; + } + + public void setDeleteArchiveFiles(boolean b) { + deleteArchiveFiles = b; + } + public boolean isDeleteArchiveFiles() { + return deleteArchiveFiles; + } + // -------------------------------------------------------- Private Methods - + /** + * Deletes old access log files. Fields that are examined are files that start with + * prefix. These files are sorted by their last modified date + * + */ + private synchronized void deleteOldFiles() { + if (!deleteArchiveFiles) + return; + File dir = getDirectoryAsFile(); + File[] files = dir.listFiles(new FilenameFilter() { + public boolean accept(File dir, String name) { + return name.startsWith(prefix); + } + }); + if (files != null) { + Arrays.sort(files, new FileComparator()); + for (int i = 0; i < files.length - archiveFileCount; i++) { + /* + * This cannot happen in the initial implementation, since this is called only + * by close() and currentLogFile will be null. However, we will add the safeguard. + */ + if (files[i].equals(currentLogFile)) + continue; + boolean b = files[i].delete(); + if (!b) + log.error(sm.getString("accessLogValve.deleteArchiveFail",files[i])); + } + } + } /** * Close the currently open log file (if any) */ @@ -1005,6 +1058,7 @@ writer = null; dateStamp = ""; currentLogFile = null; + deleteOldFiles(); } @@ -1070,16 +1124,19 @@ } - + private synchronized File getDirectoryAsFile() { + File dir = new File(directory); + if (!dir.isAbsolute()) { + dir = new File(System.getProperty(Globals.CATALINA_BASE_PROP), directory); + } + return dir; + } /** * Open the new log file for the date specified by dateStamp. */ protected synchronized void open() { // Create the directory if necessary - File dir = new File(directory); - if (!dir.isAbsolute()) { - dir = new File(System.getProperty(Globals.CATALINA_BASE_PROP), directory); - } + File dir = getDirectoryAsFile(); if (!dir.mkdirs() && !dir.isDirectory()) { log.error(sm.getString("accessLogValve.openDirFail", dir)); } @@ -2047,4 +2104,16 @@ return new StringElement("???" + pattern + "???"); } } + private final static class FileComparator implements Comparator { + public int compare(File o1, File o2) { + int rval; + if (o1.lastModified() < o2.lastModified()) + rval = -1; + else if (o1.lastModified() > o2.lastModified()) + rval = 1; + else + rval = 0; + return rval; + } + } }