[logback-dev] svn commit: r2418 - in logback/trunk/logback-core/src: main/java/ch/qos/logback/core/rolling main/java/ch/qos/logback/core/rolling/helper test/java/ch/qos/logback/core/rolling test/java/ch/qos/logback/core/rolling/helper

noreply.ceki at qos.ch noreply.ceki at qos.ch
Fri Aug 7 12:01:17 CEST 2009


Author: ceki
Date: Fri Aug  7 12:01:16 2009
New Revision: 2418

Added:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/ArchiveRemover.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/DefaultArchiveRemover.java
      - copied, changed from r2417, /logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/TimeBasedCleaner.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingWithHistoryCleanTest.java
      - copied, changed from r2417, /logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingWithCleanTest.java
Removed:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/TimeBasedCleaner.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingWithCleanTest.java
Modified:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/DefaultTimeBasedFileNamingAndTriggeringPolicy.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedFileNamingAndTriggeringPolicy.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedFileNamingAndTriggeringPolicyBase.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedRollingPolicy.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/PackageTest.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/DatePatternToRegexTest.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/FileNamePatternTest.java

Log:
- The TimeBasedCleaner class was refactored into the ArchiverRemover interface and DefailtArchiverRemover class.
- Providing an archive cleaner is now a responsibility of any TimeBasedFileNamingAndTriggeringPolicy



Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/DefaultTimeBasedFileNamingAndTriggeringPolicy.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/DefaultTimeBasedFileNamingAndTriggeringPolicy.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/DefaultTimeBasedFileNamingAndTriggeringPolicy.java	Fri Aug  7 12:01:16 2009
@@ -12,6 +12,8 @@
 import java.io.File;
 import java.util.Date;
 
+import ch.qos.logback.core.rolling.helper.DefaultArchiveRemover;
+
 /**
  * 
  * @author Ceki Gülcü
@@ -20,15 +22,15 @@
  */
 public class DefaultTimeBasedFileNamingAndTriggeringPolicy<E> extends TimeBasedFileNamingAndTriggeringPolicyBase<E> {
 
-
-
+  
+  
   @Override
   public void start() {
     super.start();
+    archiveRemover = new DefaultArchiveRemover(tbrp.fileNamePattern, rc);
     started = true;
   }
   
-
   public boolean isTriggeringEvent(File activeFile, final E event) {
     long time = getCurrentTime();
     if (time >= nextCheck) {

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP.java	Fri Aug  7 12:01:16 2009
@@ -102,5 +102,4 @@
     this.maxFileSizeAsString = maxFileSize;
     this.maxFileSize = FileSize.valueOf(maxFileSize);
   }
-
 }

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedFileNamingAndTriggeringPolicy.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedFileNamingAndTriggeringPolicy.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedFileNamingAndTriggeringPolicy.java	Fri Aug  7 12:01:16 2009
@@ -12,7 +12,7 @@
 
 import java.util.Date;
 
-import ch.qos.logback.core.rolling.helper.RollingCalendar;
+import ch.qos.logback.core.rolling.helper.ArchiveRemover;
 import ch.qos.logback.core.spi.ContextAware;
 
 /**
@@ -50,12 +50,10 @@
   String getCurrentPeriodsFileNameWithoutCompressionSuffix();
 
   /**
-   * Return the rolling calendar used by this instance.
-   * 
-   * @return
+   * Return the archive remover appropriate for this instance.
    */
-  RollingCalendar getRollingCalendar();
-
+  public ArchiveRemover getArchiveRemover();
+  
   /**
    * Return the current time which is usually the value returned by
    * System.currentMillis(). However, for <b>testing</b> purposed this value

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedFileNamingAndTriggeringPolicyBase.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedFileNamingAndTriggeringPolicyBase.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedFileNamingAndTriggeringPolicyBase.java	Fri Aug  7 12:01:16 2009
@@ -2,6 +2,7 @@
 
 import java.util.Date;
 
+import ch.qos.logback.core.rolling.helper.ArchiveRemover;
 import ch.qos.logback.core.rolling.helper.DateTokenConverter;
 import ch.qos.logback.core.rolling.helper.RollingCalendar;
 import ch.qos.logback.core.spi.ContextAwareBase;
@@ -11,7 +12,7 @@
 
   protected TimeBasedRollingPolicy<E> tbrp;
   
-  
+  protected ArchiveRemover archiveRemover = null;
   protected String elapsedPeriodsFileName;
   protected RollingCalendar rc;
   
@@ -21,7 +22,6 @@
   protected boolean isTimeForced = false;
   protected Date dateInCurrentPeriod = null;
   
-  
   protected long nextCheck;
   protected boolean started = false;
 
@@ -104,8 +104,8 @@
 
   }
 
-  public RollingCalendar getRollingCalendar() {
-    return rc;
+  public ArchiveRemover getArchiveRemover() {
+    return archiveRemover;
   }
 
 }

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedRollingPolicy.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedRollingPolicy.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedRollingPolicy.java	Fri Aug  7 12:01:16 2009
@@ -14,12 +14,12 @@
 import java.util.concurrent.Future;
 
 import ch.qos.logback.core.CoreConstants;
+import ch.qos.logback.core.rolling.helper.ArchiveRemover;
 import ch.qos.logback.core.rolling.helper.AsynchronousCompressor;
 import ch.qos.logback.core.rolling.helper.CompressionMode;
 import ch.qos.logback.core.rolling.helper.Compressor;
 import ch.qos.logback.core.rolling.helper.FileNamePattern;
 import ch.qos.logback.core.rolling.helper.RenameUtil;
-import ch.qos.logback.core.rolling.helper.TimeBasedCleaner;
 
 /**
  * <code>TimeBasedRollingPolicy</code> is both easy to configure and quite
@@ -38,15 +38,16 @@
 
   // WCS: without compression suffix
   FileNamePattern fileNamePatternWCS;
-  
+
   private Compressor compressor;
   private RenameUtil renameUtil = new RenameUtil();
   Future<?> future;
 
   private int maxHistory = NO_DELETE_HISTORY;
-  private TimeBasedCleaner tbCleaner;
+  private ArchiveRemover archiveRemover;
 
   TimeBasedFileNamingAndTriggeringPolicy<E> timeBasedTriggering;
+
   public void start() {
     // set the LR for our utility object
     renameUtil.setContext(this.context);
@@ -65,26 +66,30 @@
     compressor = new Compressor(compressionMode);
     compressor.setContext(context);
 
-    fileNamePatternWCS = new FileNamePattern(computeFileNameStr_WCS(fileNamePatternStr,
-        compressionMode), this.context);
+    fileNamePatternWCS = new FileNamePattern(computeFileNameStr_WCS(
+        fileNamePatternStr, compressionMode), this.context);
 
     addInfo("Will use the pattern " + fileNamePatternWCS
         + " for the active file");
 
-    if(timeBasedTriggering == null) {
+    if (timeBasedTriggering == null) {
       timeBasedTriggering = new DefaultTimeBasedFileNamingAndTriggeringPolicy<E>();
     }
     timeBasedTriggering.setContext(context);
     timeBasedTriggering.setTimeBasedRollingPolicy(this);
     timeBasedTriggering.start();
 
+    // the maxHistory property is given to TimeBasedRollingPolicy instead of to
+    // the TimeBasedFileNamingAndTriggeringPolicy. This makes it more convenient
+    // for the user at the cost of inconsistency at the level of this code.
     if (maxHistory != NO_DELETE_HISTORY) {
-      tbCleaner = new TimeBasedCleaner(fileNamePattern, timeBasedTriggering.getRollingCalendar(),
-          maxHistory);
+      archiveRemover = timeBasedTriggering.getArchiveRemover();
+      archiveRemover.setMaxHistory(maxHistory);
     }
   }
 
-  public void setTimeBasedFileNamingAndTriggeringPolicy(TimeBasedFileNamingAndTriggeringPolicy<E> timeBasedTriggering) {
+  public void setTimeBasedFileNamingAndTriggeringPolicy(
+      TimeBasedFileNamingAndTriggeringPolicy<E> timeBasedTriggering) {
     this.timeBasedTriggering = timeBasedTriggering;
   }
 
@@ -92,7 +97,6 @@
     return timeBasedTriggering;
   }
 
-  
   static String computeFileNameStr_WCS(String fileNamePatternStr,
       CompressionMode compressionMode) {
     int len = fileNamePatternStr.length();
@@ -127,8 +131,8 @@
       }
     }
 
-    if (tbCleaner != null) {
-      tbCleaner.clean(new Date(timeBasedTriggering.getCurrentTime()));
+    if (archiveRemover != null) {
+      archiveRemover.clean(new Date(timeBasedTriggering.getCurrentTime()));
     }
   }
 
@@ -171,7 +175,8 @@
     if (parentsRawFileProperty != null) {
       return parentsRawFileProperty;
     } else {
-      return timeBasedTriggering.getCurrentPeriodsFileNameWithoutCompressionSuffix();
+      return timeBasedTriggering
+          .getCurrentPeriodsFileNameWithoutCompressionSuffix();
     }
   }
 

Added: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/ArchiveRemover.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/ArchiveRemover.java	Fri Aug  7 12:01:16 2009
@@ -0,0 +1,22 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 2000-2009, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package ch.qos.logback.core.rolling.helper;
+
+import java.util.Date;
+
+/**
+ * Given a date remove older archived log files.
+ * 
+ * @author Ceki G&uuml;lc&uuml;
+ */
+public interface ArchiveRemover {
+  public void clean(Date now);
+  public void setMaxHistory(int maxHistory);
+} 
\ No newline at end of file

Copied: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/DefaultArchiveRemover.java (from r2417, /logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/TimeBasedCleaner.java)
==============================================================================
--- /logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/TimeBasedCleaner.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/DefaultArchiveRemover.java	Fri Aug  7 12:01:16 2009
@@ -13,22 +13,20 @@
 import java.io.File;
 import java.util.Date;
 
-public class TimeBasedCleaner {
+public class DefaultArchiveRemover implements ArchiveRemover {
 
   FileNamePattern fileNamePattern;
   RollingCalendar rc;
-  int numberOfPeriods;
-
-  public TimeBasedCleaner(FileNamePattern fileNamePattern, RollingCalendar rc,
-      int numberOfPeriods) {
+  int maxHistory;
+  int periodOffset;
+  
+  public DefaultArchiveRemover(FileNamePattern fileNamePattern, RollingCalendar rc) {
     this.fileNamePattern = fileNamePattern;
     this.rc = rc;
-    //
-    this.numberOfPeriods = -numberOfPeriods -1;
   }
 
   public void clean(Date now) {
-    Date date2delete = rc.getRelativeDate(now, numberOfPeriods);
+    Date date2delete = rc.getRelativeDate(now, periodOffset);
 
     String filename = fileNamePattern.convert(date2delete);
 
@@ -39,4 +37,9 @@
     }
   }
 
+  public void setMaxHistory(int maxHistory) {
+    this.maxHistory = maxHistory;
+    this.periodOffset = -maxHistory -1;
+  }
+
 }

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/PackageTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/PackageTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/PackageTest.java	Fri Aug  7 12:01:16 2009
@@ -5,7 +5,7 @@
 
 @RunWith(Suite.class)
 @Suite.SuiteClasses( { RenamingTest.class, SizeBasedRollingTest.class,
-    TimeBasedRollingTest.class, TimeBasedRollingWithCleanTest.class,
+    TimeBasedRollingTest.class, TimeBasedRollingWithHistoryCleanTest.class,
     MultiThreadedRollingTest.class,
     SizeAndTimeBasedFNATP_Test.class,
     ch.qos.logback.core.rolling.helper.PackageTest.class })

Copied: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingWithHistoryCleanTest.java (from r2417, /logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingWithCleanTest.java)
==============================================================================
--- /logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingWithCleanTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingWithHistoryCleanTest.java	Fri Aug  7 12:01:16 2009
@@ -15,7 +15,7 @@
 import ch.qos.logback.core.layout.EchoLayout;
 import ch.qos.logback.core.util.CoreTestConstants;
 
-public class TimeBasedRollingWithCleanTest {
+public class TimeBasedRollingWithHistoryCleanTest {
 
   Context context = new ContextBase();
   EchoLayout<Object> layout = new EchoLayout<Object>();

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/DatePatternToRegexTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/DatePatternToRegexTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/DatePatternToRegexTest.java	Fri Aug  7 12:01:16 2009
@@ -95,7 +95,7 @@
     }
     String regex = dtc.toSRegex();
     //System.out.println("expected="+expected);
-    System.out.println(regex);
+    //System.out.println(regex);
     assertTrue("[" + expected + "] does not match regex [" + regex + "]",
         expected.matches(regex));
   }

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/FileNamePatternTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/FileNamePatternTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/FileNamePatternTest.java	Fri Aug  7 12:01:16 2009
@@ -89,7 +89,7 @@
     pp = new FileNamePattern("%d{yyyy.MM.dd HH:mm} foo", context);
     assertEquals("2003.05.20 17:55 foo", pp.convert(cal.getTime()));
 
-  }
+  } 
 
   @Test
   public void withBackslash() {


More information about the logback-dev mailing list