Author: markt Date: Mon Mar 19 18:35:03 2018 New Revision: 1827223 URL: http://svn.apache.org/viewvc?rev=1827223&view=rev Log: Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=52688 Add support for the maxDays attribute to specify the maximum number of days for which rotated access log files should be retained before deletion.
Modified: tomcat/trunk/java/org/apache/catalina/valves/AccessLogValve.java tomcat/trunk/java/org/apache/catalina/valves/LocalStrings.properties tomcat/trunk/webapps/docs/changelog.xml tomcat/trunk/webapps/docs/config/valve.xml Modified: tomcat/trunk/java/org/apache/catalina/valves/AccessLogValve.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/valves/AccessLogValve.java?rev=1827223&r1=1827222&r2=1827223&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/valves/AccessLogValve.java (original) +++ tomcat/trunk/java/org/apache/catalina/valves/AccessLogValve.java Mon Mar 19 18:35:03 2018 @@ -87,7 +87,7 @@ public class AccessLogValve extends Abst /** * The prefix that is added to log file filenames. */ - protected String prefix = "access_log"; + protected volatile String prefix = "access_log"; /** @@ -111,7 +111,7 @@ public class AccessLogValve extends Abst /** * The suffix that is added to log file filenames. */ - protected String suffix = ""; + protected volatile String suffix = ""; /** @@ -156,9 +156,26 @@ public class AccessLogValve extends Abst */ protected volatile String encoding = null; + /** + * The number of days to retain the access log files before they are + * removed. + */ + private int maxDays = -1; + private volatile boolean checkForOldLogs = false; + // ------------------------------------------------------------- Properties + public int getMaxDays() { + return maxDays; + } + + + public void setMaxDays(int maxDays) { + this.maxDays = maxDays; + } + + /** * @return the directory in which we create log files. */ @@ -358,6 +375,50 @@ public class AccessLogValve extends Abst buffered) { writer.flush(); } + + int maxDays = this.maxDays; + String prefix = this.prefix; + String suffix = this.suffix; + + if (rotatable && checkForOldLogs && maxDays > 0) { + long deleteIfLastModifiedBefore = + System.currentTimeMillis() - (maxDays * 24L * 60 * 60 * 1000); + File dir = getDirectoryFile(); + if (dir.isDirectory()) { + String[] oldAccessLogs = dir.list(); + + if (oldAccessLogs != null) { + for (String oldAccessLog : oldAccessLogs) { + boolean match = false; + + if (prefix != null && prefix.length() > 0) { + if (!oldAccessLog.startsWith(prefix)) { + continue; + } + match = true; + } + + if (suffix != null && suffix.length() > 0) { + if (!oldAccessLog.endsWith(suffix)) { + continue; + } + match = true; + } + + if (match) { + File file = new File(dir, oldAccessLog); + if (file.isFile() && file.lastModified() < deleteIfLastModifiedBefore) { + if (!file.delete()) { + log.warn(sm.getString( + "accessLogValve.deleteFail", file.getAbsolutePath())); + } + } + } + } + } + } + checkForOldLogs = false; + } } /** @@ -423,6 +484,15 @@ public class AccessLogValve extends Abst // -------------------------------------------------------- Private Methods + private File getDirectoryFile() { + File dir = new File(directory); + if (!dir.isAbsolute()) { + dir = new File(getContainer().getCatalinaBase(), directory); + } + return dir; + } + + /** * Create a File object based on the current log file name. * Directories are created as needed but the underlying file @@ -432,12 +502,8 @@ public class AccessLogValve extends Abst * @return the log file object */ private File getLogFile(boolean useDateStamp) { - // Create the directory if necessary - File dir = new File(directory); - if (!dir.isAbsolute()) { - dir = new File(getContainer().getCatalinaBase(), directory); - } + File dir = getDirectoryFile(); if (!dir.mkdirs() && !dir.isDirectory()) { log.error(sm.getString("accessLogValve.openDirFail", dir)); } @@ -592,6 +658,10 @@ public class AccessLogValve extends Abst currentLogFile = null; log.error(sm.getString("accessLogValve.openFail", pathname), e); } + // Rotating a log file will always trigger a new file to be opened so + // when a new file is opened, check to see if any old files need to be + // removed. + checkForOldLogs = true; } /** Modified: tomcat/trunk/java/org/apache/catalina/valves/LocalStrings.properties URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/valves/LocalStrings.properties?rev=1827223&r1=1827222&r2=1827223&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/valves/LocalStrings.properties (original) +++ tomcat/trunk/java/org/apache/catalina/valves/LocalStrings.properties Mon Mar 19 18:35:03 2018 @@ -19,6 +19,7 @@ jdbcAccessLogValve.exception=Exception p # Access log valve accessLogValve.openFail=Failed to open access log file [{0}] accessLogValve.closeFail=Failed to close access log file +accessLogValve.deleteFail=Failed to delete old access log [{0}] accessLogValve.openDirFail=Failed to create directory [{0}] for access logs accessLogValve.rotateFail=Failed to rotate access log accessLogValve.renameFail=Failed to rename access log from [{0}] to [{1}] Modified: tomcat/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1827223&r1=1827222&r2=1827223&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/changelog.xml (original) +++ tomcat/trunk/webapps/docs/changelog.xml Mon Mar 19 18:35:03 2018 @@ -54,6 +54,12 @@ (markt) </fix> <fix> + <bug>52688</bug>: Add support for the <code>maxDays</code> attribute to + the <code>AccessLogValve</code> and <code>ExtendedAccessLogValve</code>. + This allows the maximum number of days for which rotated access logs + should be retained before deletion to be defined. (markt) + </fix> + <fix> Ensure the MBean names for the <code>SSLHostConfig</code> and <code>SSLHostConfigCertificate</code> are correctly formed when the <code>Connector</code> is bound to a specific IP address. (markt) Modified: tomcat/trunk/webapps/docs/config/valve.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/config/valve.xml?rev=1827223&r1=1827222&r2=1827223&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/config/valve.xml (original) +++ tomcat/trunk/webapps/docs/config/valve.xml Mon Mar 19 18:35:03 2018 @@ -194,6 +194,12 @@ </p> </attribute> + <attribute name="maxDays" required="false"> + <p>The maximum number of days rotated access logs will be retained for + before being deleted. If not specified, the default value of + <code>-1</code> will be used which means never delete old files.</p> + </attribute> + <attribute name="maxLogMessageBufferSize" required="false"> <p>Log message buffers are usually recycled and re-used. To prevent excessive memory usage, if a buffer grows beyond this size it will be --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org