Uploaded image for project: 'logback'
  1. logback
  2. LOGBACK-50

TimeBasedRollingPolicy do NOT work as expected

    XMLWordPrintable

Details

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Blocker Blocker
    • None
    • None
    • logback-core
    • None
    • Operating System: Linux
      Platform: All

    • 80

    Description

      When I use TimeBasedRollingPolicy as the rolling & triggering policy for the RollingFileAppender, only the last log line appears in the new log file after the 1st rolling trigger.

      The reason is that the TimeBasedRollingPolicy kept rolling for every log event after the 1st rolling trigger. It causes by a bug in isTriggeringEvent() method - the method uses the un-initialized value of 'currentTime' (which is 0) to set the time of the 'lastCheck' Date object.

      Original method :

      public boolean isTriggeringEvent(File activeFile, final Object event) {
      //currentTime= System.currentTimeMillis();

      if (getCurrentTime() >= nextCheck)

      { //addInfo("Time to trigger roll-over"); // We set the elapsedPeriodsFileName before we set the 'lastCheck' variable // The elapsedPeriodsFileName corresponds to the file name of the period // that just elapsed. elapsedPeriodsFileName = activeFileNamePattern.convertDate(lastCheck); //addInfo("elapsedPeriodsFileName set to "+elapsedPeriodsFileName); lastCheck.setTime(currentTime); nextCheck = rc.getNextCheckMillis(lastCheck); Date x = new Date(); x.setTime(nextCheck); //addInfo("Next check on "+ x); return true; }

      else

      { return false; }
      }

      Suggested Changes - uses a local long variable to store the value of getCurrentTime() and uses it in the method for comparison and assignment:

      public boolean isTriggeringEvent(File activeFile, final Object event) {
      //currentTime= System.currentTimeMillis();

      long curT = getCurrentTime();

      if (curT >= nextCheck) { //addInfo("Time to trigger roll-over"); // We set the elapsedPeriodsFileName before we set the 'lastCheck' variable // The elapsedPeriodsFileName corresponds to the file name of the period // that just elapsed. elapsedPeriodsFileName = activeFileNamePattern.convertDate(lastCheck); //addInfo("elapsedPeriodsFileName set to "+elapsedPeriodsFileName); lastCheck.setTime(curT); nextCheck = rc.getNextCheckMillis(lastCheck); //Date x = new Date(); //x.setTime(nextCheck); //addInfo("Next check on "+ x); return true; } else { return false; }

      }

      Attachments

        Activity

          People

            logback-dev Logback dev list
            ekyung Eric
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: