Bug 46851

Summary: DailyRollingFileAppender with DatePattern='.'yyyy-ww gives monthly log rotation if the default locale is Locale.GERMAN
Product: Log4j - Now in Jira Reporter: Olaf Strozyk <strozyk>
Component: AppenderAssignee: log4j-dev <log4j-dev>
Status: RESOLVED DUPLICATE    
Severity: normal CC: strozyk
Priority: P2 Keywords: PatchAvailable
Version: 1.2   
Target Milestone: ---   
Hardware: All   
OS: All   

Description Olaf Strozyk 2009-03-13 08:36:05 UTC
Using the DailyRollingFileAppender with DatePattern='.'yyyy-ww gives monthly log rotation if the default locale of the JVM is Locale.GERMAN.

Expected is weekly log rotation as documented in JavaDoc.


I think the reason for this issue is that in method DailyRollingFileAppender.computeCheckPeriod:
-the RollingCalendar instance is explicitly constructed with Locale.ENGLISH, which gives SUNDAY as first day of week
-in the SimpleDateFormat instance, only timezone is set. Therefore, in germany simpleDateFormat uses a Calendar with Locale.GERMAN, which gives MONDAY as first day of week
Then, rollingCalendar.getNextCheckMillis() returns a Sunday, which belongs to the same week as epoch from SimpleDateFormat's point of view.

I suggest to fix the problem by removing -line and adding +lines as follows:

  int computeCheckPeriod() {
    RollingCalendar rollingCalendar = new RollingCalendar(gmtTimeZone, Locale.ENGLISH);
    // set sate to 1970-01-01 00:00:00 GMT
    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
+       // do all date formatting in GMT and with ENGLISH locale settings
+       Calendar formatCal = Calendar.getInstance(gmtTimeZone, Locale.ENGLISH);
+       simpleDateFormat.setCalendar(formatCal);
        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...
  }
Comment 1 Curt Arnold 2009-10-10 11:24:26 UTC
Code fragment appears to predate the patch for 40888.  Believe the suggested change and the 40888 change are incompatibility.  Please reopen (or file a new bug) if the current SVN HEAD does not resolve the issue.

*** This bug has been marked as a duplicate of bug 40888 ***