Index: DailyRollingFileAppender.java =================================================================== --- DailyRollingFileAppender.java (revision 572567) +++ DailyRollingFileAppender.java (working copy) @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -157,10 +157,10 @@ scheduledFilename variable when the next interval is entered. For example, if the rollover period is one hour, the log file will be renamed to the value of "scheduledFilename" at the beginning of - the next hour. + the next hour. The precise time when a rollover occurs depends on logging - activity. + activity. */ private String scheduledFilename; @@ -192,7 +192,7 @@ */ public DailyRollingFileAppender (Layout layout, String filename, - String datePattern) throws IOException { + String datePattern) throws IOException { super(layout, filename, true); this.datePattern = datePattern; activateOptions(); @@ -225,7 +225,7 @@ } else { LogLog.error("Either File or DatePattern options are not set for appender [" - +name+"]."); + +name+"]."); } } @@ -236,23 +236,23 @@ break; case TOP_OF_HOUR: LogLog.debug("Appender ["+name - +"] to be rolled on top of every hour."); + +"] to be rolled on top of every hour."); break; case HALF_DAY: LogLog.debug("Appender ["+name - +"] to be rolled at midday and midnight."); + +"] to be rolled at midday and midnight."); break; case TOP_OF_DAY: LogLog.debug("Appender ["+name - +"] to be rolled at midnight."); + +"] to be rolled at midnight."); break; case TOP_OF_WEEK: LogLog.debug("Appender ["+name - +"] to be rolled at start of week."); + +"] to be rolled at start of week."); break; case TOP_OF_MONTH: LogLog.debug("Appender ["+name - +"] to be rolled at start of every month."); + +"] to be rolled at start of every month."); break; default: LogLog.warn("Unknown periodicity for appender ["+name+"]."); @@ -275,16 +275,16 @@ Date epoch = new Date(0); if(datePattern != null) { for(int i = TOP_OF_MINUTE; i <= TOP_OF_MONTH; i++) { - SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern); - simpleDateFormat.setTimeZone(gmtTimeZone); // do all date formatting in GMT - String r0 = simpleDateFormat.format(epoch); - rollingCalendar.setType(i); - Date next = new Date(rollingCalendar.getNextCheckMillis(epoch)); - String r1 = simpleDateFormat.format(next); - //System.out.println("Type = "+i+", r0 = "+r0+", r1 = "+r1); - if(r0 != null && r1 != null && !r0.equals(r1)) { - return i; - } + SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern); + simpleDateFormat.setTimeZone(gmtTimeZone); // do all date formatting in GMT + String r0 = simpleDateFormat.format(epoch); + rollingCalendar.setType(i); + Date next = new Date(rollingCalendar.getNextCheckMillis(epoch)); + String r1 = simpleDateFormat.format(next); + //System.out.println("Type = "+i+", r0 = "+r0+", r1 = "+r1); + if(r0 != null && r1 != null && !r0.equals(r1)) { + return i; + } } } return TOP_OF_TROUBLE; // Deliberately head for trouble... @@ -319,16 +319,19 @@ File file = new File(fileName); boolean result = file.renameTo(target); + boolean continueToAppend = false; if(result) { LogLog.debug(fileName +" -> "+ scheduledFilename); } else { - LogLog.error("Failed to rename ["+fileName+"] to ["+scheduledFilename+"]."); + // if file rename failed, reopen file with append = true + LogLog.warn("Failed to rename ["+fileName+"] to ["+scheduledFilename+"]."); + continueToAppend = true; } try { // This will also close the file. This is OK since multiple // close operations are safe. - this.setFile(fileName, false, this.bufferedIO, this.bufferSize); + this.setFile(fileName, continueToAppend, this.bufferedIO, this.bufferSize); } catch(IOException e) { errorHandler.error("setFile("+fileName+", false) call failed."); @@ -350,10 +353,10 @@ now.setTime(n); nextCheck = rc.getNextCheckMillis(now); try { - rollOver(); + rollOver(); } catch(IOException ioe) { - LogLog.error("rollOver() failed.", ioe); + LogLog.error("rollOver() failed.", ioe); } } super.subAppend(event); @@ -363,7 +366,7 @@ /** * RollingCalendar is a helper class to DailyRollingFileAppender. * Given a periodicity type and the current time, it computes the - * start of the next interval. + * start of the next interval. * */ class RollingCalendar extends GregorianCalendar { private static final long serialVersionUID = -3560331770601814177L; @@ -372,11 +375,11 @@ RollingCalendar() { super(); - } + } RollingCalendar(TimeZone tz, Locale locale) { super(tz, locale); - } + } void setType(int type) { this.type = type; @@ -391,53 +394,53 @@ switch(type) { case DailyRollingFileAppender.TOP_OF_MINUTE: - this.set(Calendar.SECOND, 0); - this.set(Calendar.MILLISECOND, 0); - this.add(Calendar.MINUTE, 1); - break; + this.set(Calendar.SECOND, 0); + this.set(Calendar.MILLISECOND, 0); + this.add(Calendar.MINUTE, 1); + break; case DailyRollingFileAppender.TOP_OF_HOUR: - this.set(Calendar.MINUTE, 0); - this.set(Calendar.SECOND, 0); - this.set(Calendar.MILLISECOND, 0); - this.add(Calendar.HOUR_OF_DAY, 1); - break; + this.set(Calendar.MINUTE, 0); + this.set(Calendar.SECOND, 0); + this.set(Calendar.MILLISECOND, 0); + this.add(Calendar.HOUR_OF_DAY, 1); + break; case DailyRollingFileAppender.HALF_DAY: - this.set(Calendar.MINUTE, 0); - this.set(Calendar.SECOND, 0); - this.set(Calendar.MILLISECOND, 0); - int hour = get(Calendar.HOUR_OF_DAY); - if(hour < 12) { - this.set(Calendar.HOUR_OF_DAY, 12); - } else { - this.set(Calendar.HOUR_OF_DAY, 0); - this.add(Calendar.DAY_OF_MONTH, 1); - } - break; + this.set(Calendar.MINUTE, 0); + this.set(Calendar.SECOND, 0); + this.set(Calendar.MILLISECOND, 0); + int hour = get(Calendar.HOUR_OF_DAY); + if(hour < 12) { + this.set(Calendar.HOUR_OF_DAY, 12); + } else { + this.set(Calendar.HOUR_OF_DAY, 0); + this.add(Calendar.DAY_OF_MONTH, 1); + } + break; case DailyRollingFileAppender.TOP_OF_DAY: - this.set(Calendar.HOUR_OF_DAY, 0); - this.set(Calendar.MINUTE, 0); - this.set(Calendar.SECOND, 0); - this.set(Calendar.MILLISECOND, 0); - this.add(Calendar.DATE, 1); - break; + this.set(Calendar.HOUR_OF_DAY, 0); + this.set(Calendar.MINUTE, 0); + this.set(Calendar.SECOND, 0); + this.set(Calendar.MILLISECOND, 0); + this.add(Calendar.DATE, 1); + break; case DailyRollingFileAppender.TOP_OF_WEEK: - this.set(Calendar.DAY_OF_WEEK, getFirstDayOfWeek()); - this.set(Calendar.HOUR_OF_DAY, 0); - this.set(Calendar.MINUTE, 0); - this.set(Calendar.SECOND, 0); - this.set(Calendar.MILLISECOND, 0); - this.add(Calendar.WEEK_OF_YEAR, 1); - break; + this.set(Calendar.DAY_OF_WEEK, getFirstDayOfWeek()); + this.set(Calendar.HOUR_OF_DAY, 0); + this.set(Calendar.MINUTE, 0); + this.set(Calendar.SECOND, 0); + this.set(Calendar.MILLISECOND, 0); + this.add(Calendar.WEEK_OF_YEAR, 1); + break; case DailyRollingFileAppender.TOP_OF_MONTH: - this.set(Calendar.DATE, 1); - this.set(Calendar.HOUR_OF_DAY, 0); - this.set(Calendar.MINUTE, 0); - this.set(Calendar.SECOND, 0); - this.set(Calendar.MILLISECOND, 0); - this.add(Calendar.MONTH, 1); - break; + this.set(Calendar.DATE, 1); + this.set(Calendar.HOUR_OF_DAY, 0); + this.set(Calendar.MINUTE, 0); + this.set(Calendar.SECOND, 0); + this.set(Calendar.MILLISECOND, 0); + this.add(Calendar.MONTH, 1); + break; default: - throw new IllegalStateException("Unknown periodicity type."); + throw new IllegalStateException("Unknown periodicity type."); } return getTime(); }