logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1563201 - /logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java
Date Fri, 31 Jan 2014 18:30:33 GMT
Author: ggregory
Date: Fri Jan 31 18:30:33 2014
New Revision: 1563201

URL: http://svn.apache.org/r1563201
Log:
Sort members and put all the static factory method next to the ctors.

Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java?rev=1563201&r1=1563200&r2=1563201&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java
Fri Jan 31 18:30:33 2014
@@ -85,6 +85,48 @@ public class DefaultRolloverStrategy imp
     private static final int DEFAULT_WINDOW_SIZE = 7;
 
     /**
+     * Create the DefaultRolloverStrategy.
+     * @param max The maximum number of files to keep.
+     * @param min The minimum number of files to keep.
+     * @param fileIndex If set to "max" (the default), files with a higher index will be
newer than files with a
+     * smaller index. If set to "min", file renaming and the counter will follow the Fixed
Window strategy.
+     * @param compressionLevelStr The compression level, 0 (less) through 9 (more); applies
only to ZIP files.
+     * @param config The Configuration.
+     * @return A DefaultRolloverStrategy.
+     */
+    @PluginFactory
+    public static DefaultRolloverStrategy createStrategy(
+            @PluginAttribute("max") final String max,
+            @PluginAttribute("min") final String min,
+            @PluginAttribute("fileIndex") final String fileIndex,
+            @PluginAttribute("compressionLevel") final String compressionLevelStr,
+            @PluginConfiguration final Configuration config) {
+        final boolean useMax = fileIndex == null ? true : fileIndex.equalsIgnoreCase("max");
+        int minIndex;
+        if (min != null) {
+            minIndex = Integer.parseInt(min);
+            if (minIndex < 1) {
+                LOGGER.error("Minimum window size too small. Limited to " + MIN_WINDOW_SIZE);
+                minIndex = MIN_WINDOW_SIZE;
+            }
+        } else {
+            minIndex = MIN_WINDOW_SIZE;
+        }
+        int maxIndex;
+        if (max != null) {
+            maxIndex = Integer.parseInt(max);
+            if (maxIndex < minIndex) {
+                maxIndex = minIndex < DEFAULT_WINDOW_SIZE ? DEFAULT_WINDOW_SIZE : minIndex;
+                LOGGER.error("Maximum window size must be greater than the minimum windows
size. Set to " + maxIndex);
+            }
+        } else {
+            maxIndex = DEFAULT_WINDOW_SIZE;
+        }
+        final int compressionLevel = Integers.parseInt(compressionLevelStr, Deflater.DEFAULT_COMPRESSION);
+        return new DefaultRolloverStrategy(minIndex, maxIndex, useMax, compressionLevel,
config.getStrSubstitutor());
+    }
+
+    /**
      * Index for oldest retained log file.
      */
     private final int maxIndex;
@@ -95,9 +137,9 @@ public class DefaultRolloverStrategy imp
     private final int minIndex;
 
     private final boolean useMax;
-
-    private final StrSubstitutor subst;
     
+    private final StrSubstitutor subst;
+
     private final int compressionLevel;
 
     /**
@@ -113,83 +155,51 @@ public class DefaultRolloverStrategy imp
         this.subst = subst;
     }
 
-    /**
-     * Perform the rollover.
-     * @param manager The RollingFileManager name for current active log file.
-     * @return A RolloverDescription.
-     * @throws SecurityException if an error occurs.
-     */
-    @Override
-    public RolloverDescription rollover(final RollingFileManager manager) throws SecurityException
{
-        if (maxIndex >= 0) {
-            int fileIndex;
-
-            if ((fileIndex = purge(minIndex, maxIndex, manager)) < 0) {
-                return null;
-            }
-
-            final StringBuilder buf = new StringBuilder();
-            manager.getPatternProcessor().formatFileName(subst, buf, fileIndex);
-            final String currentFileName = manager.getFileName();
-
-            String renameTo = buf.toString();
-            final String compressedName = renameTo;
-            Action compressAction = null;
-
-            if (renameTo.endsWith(".gz")) {
-                renameTo = renameTo.substring(0, renameTo.length() - 3);
-                compressAction = new GZCompressAction(new File(renameTo), new File(compressedName),
true);
-            } else if (renameTo.endsWith(".zip")) {
-                renameTo = renameTo.substring(0, renameTo.length() - 4);
-                compressAction = new ZipCompressAction(new File(renameTo), new File(compressedName),
true, 
-                        compressionLevel);
-            }
-
-            final FileRenameAction renameAction =
-                new FileRenameAction(new File(currentFileName), new File(renameTo), false);
-
-            return new RolloverDescriptionImpl(currentFileName, false, renameAction, compressAction);
-        }
-
-        return null;
-    }
-
     private int purge(final int lowIndex, final int highIndex, final RollingFileManager manager)
{
         return useMax ? purgeAscending(lowIndex, highIndex, manager) :
             purgeDescending(lowIndex, highIndex, manager);
     }
 
     /**
-     * Purge and rename old log files in preparation for rollover. The newest file will have
the smallest index, the
-     * oldest will have the highest.
+     * Purge and rename old log files in preparation for rollover. The oldest file will have
the smallest index,
+     * the newest the highest.
      *
      * @param lowIndex  low index
      * @param highIndex high index.  Log file associated with high index will be deleted
if needed.
      * @param manager The RollingFileManager
      * @return true if purge was successful and rollover should be attempted.
      */
-    private int purgeDescending(final int lowIndex, final int highIndex, final RollingFileManager
manager) {
+    private int purgeAscending(final int lowIndex, final int highIndex, final RollingFileManager
manager) {
         int suffixLength = 0;
 
         final List<FileRenameAction> renames = new ArrayList<FileRenameAction>();
         final StringBuilder buf = new StringBuilder();
-        manager.getPatternProcessor().formatFileName(buf, lowIndex);
+        manager.getPatternProcessor().formatFileName(buf, highIndex);
 
-        String lowFilename = subst.replace(buf);
+        String highFilename = subst.replace(buf);
 
-        if (lowFilename.endsWith(".gz")) {
+        if (highFilename.endsWith(".gz")) {
             suffixLength = 3;
-        } else if (lowFilename.endsWith(".zip")) {
+        } else if (highFilename.endsWith(".zip")) {
             suffixLength = 4;
         }
 
-        for (int i = lowIndex; i <= highIndex; i++) {
-            File toRename = new File(lowFilename);
+        int maxIndex = 0;
+
+        for (int i = highIndex; i >= lowIndex; i--) {
+            File toRename = new File(highFilename);
+            if (i == highIndex && toRename.exists()) {
+                maxIndex = highIndex;
+            } else if (maxIndex == 0 && toRename.exists()) {
+                maxIndex = i + 1;
+                break;
+            }
+
             boolean isBase = false;
 
             if (suffixLength > 0) {
                 final File toRenameBase =
-                    new File(lowFilename.substring(0, lowFilename.length() - suffixLength));
+                    new File(highFilename.substring(0, highFilename.length() - suffixLength));
 
                 if (toRename.exists()) {
                     if (toRenameBase.exists()) {
@@ -203,10 +213,10 @@ public class DefaultRolloverStrategy imp
 
             if (toRename.exists()) {
                 //
-                //    if at upper index then
+                //    if at lower index and then all slots full
                 //        attempt to delete last file
                 //        if that fails then abandon purge
-                if (i == highIndex) {
+                if (i == lowIndex) {
                     if (!toRename.delete()) {
                         return -1;
                     }
@@ -218,21 +228,27 @@ public class DefaultRolloverStrategy imp
                 //   if intermediate index
                 //     add a rename action to the list
                 buf.setLength(0);
-                manager.getPatternProcessor().formatFileName(buf, i + 1);
+                manager.getPatternProcessor().formatFileName(buf, i - 1);
 
-                final String highFilename = subst.replace(buf);
-                String renameTo = highFilename;
+                final String lowFilename = subst.replace(buf);
+                String renameTo = lowFilename;
 
                 if (isBase) {
-                    renameTo = highFilename.substring(0, highFilename.length() - suffixLength);
+                    renameTo = lowFilename.substring(0, lowFilename.length() - suffixLength);
                 }
 
                 renames.add(new FileRenameAction(toRename, new File(renameTo), true));
-                lowFilename = highFilename;
+                highFilename = lowFilename;
             } else {
-                break;
+                buf.setLength(0);
+                manager.getPatternProcessor().formatFileName(buf, i - 1);
+
+                highFilename = subst.replace(buf);
             }
         }
+        if (maxIndex == 0) {
+            maxIndex = lowIndex;
+        }
 
         //
         //   work renames backwards
@@ -249,50 +265,40 @@ public class DefaultRolloverStrategy imp
                 return -1;
             }
         }
-
-        return lowIndex;
+        return maxIndex;
     }
 
     /**
-     * Purge and rename old log files in preparation for rollover. The oldest file will have
the smallest index,
-     * the newest the highest.
+     * Purge and rename old log files in preparation for rollover. The newest file will have
the smallest index, the
+     * oldest will have the highest.
      *
      * @param lowIndex  low index
      * @param highIndex high index.  Log file associated with high index will be deleted
if needed.
      * @param manager The RollingFileManager
      * @return true if purge was successful and rollover should be attempted.
      */
-    private int purgeAscending(final int lowIndex, final int highIndex, final RollingFileManager
manager) {
+    private int purgeDescending(final int lowIndex, final int highIndex, final RollingFileManager
manager) {
         int suffixLength = 0;
 
         final List<FileRenameAction> renames = new ArrayList<FileRenameAction>();
         final StringBuilder buf = new StringBuilder();
-        manager.getPatternProcessor().formatFileName(buf, highIndex);
+        manager.getPatternProcessor().formatFileName(buf, lowIndex);
 
-        String highFilename = subst.replace(buf);
+        String lowFilename = subst.replace(buf);
 
-        if (highFilename.endsWith(".gz")) {
+        if (lowFilename.endsWith(".gz")) {
             suffixLength = 3;
-        } else if (highFilename.endsWith(".zip")) {
+        } else if (lowFilename.endsWith(".zip")) {
             suffixLength = 4;
         }
 
-        int maxIndex = 0;
-
-        for (int i = highIndex; i >= lowIndex; i--) {
-            File toRename = new File(highFilename);
-            if (i == highIndex && toRename.exists()) {
-                maxIndex = highIndex;
-            } else if (maxIndex == 0 && toRename.exists()) {
-                maxIndex = i + 1;
-                break;
-            }
-
+        for (int i = lowIndex; i <= highIndex; i++) {
+            File toRename = new File(lowFilename);
             boolean isBase = false;
 
             if (suffixLength > 0) {
                 final File toRenameBase =
-                    new File(highFilename.substring(0, highFilename.length() - suffixLength));
+                    new File(lowFilename.substring(0, lowFilename.length() - suffixLength));
 
                 if (toRename.exists()) {
                     if (toRenameBase.exists()) {
@@ -306,10 +312,10 @@ public class DefaultRolloverStrategy imp
 
             if (toRename.exists()) {
                 //
-                //    if at lower index and then all slots full
+                //    if at upper index then
                 //        attempt to delete last file
                 //        if that fails then abandon purge
-                if (i == lowIndex) {
+                if (i == highIndex) {
                     if (!toRename.delete()) {
                         return -1;
                     }
@@ -321,27 +327,21 @@ public class DefaultRolloverStrategy imp
                 //   if intermediate index
                 //     add a rename action to the list
                 buf.setLength(0);
-                manager.getPatternProcessor().formatFileName(buf, i - 1);
+                manager.getPatternProcessor().formatFileName(buf, i + 1);
 
-                final String lowFilename = subst.replace(buf);
-                String renameTo = lowFilename;
+                final String highFilename = subst.replace(buf);
+                String renameTo = highFilename;
 
                 if (isBase) {
-                    renameTo = lowFilename.substring(0, lowFilename.length() - suffixLength);
+                    renameTo = highFilename.substring(0, highFilename.length() - suffixLength);
                 }
 
                 renames.add(new FileRenameAction(toRename, new File(renameTo), true));
-                highFilename = lowFilename;
+                lowFilename = highFilename;
             } else {
-                buf.setLength(0);
-                manager.getPatternProcessor().formatFileName(buf, i - 1);
-
-                highFilename = subst.replace(buf);
+                break;
             }
         }
-        if (maxIndex == 0) {
-            maxIndex = lowIndex;
-        }
 
         //
         //   work renames backwards
@@ -358,54 +358,54 @@ public class DefaultRolloverStrategy imp
                 return -1;
             }
         }
-        return maxIndex;
-    }
 
-    @Override
-    public String toString() {
-        return "DefaultRolloverStrategy(min=" + minIndex + ", max=" + maxIndex + ")";
+        return lowIndex;
     }
 
     /**
-     * Create the DefaultRolloverStrategy.
-     * @param max The maximum number of files to keep.
-     * @param min The minimum number of files to keep.
-     * @param fileIndex If set to "max" (the default), files with a higher index will be
newer than files with a
-     * smaller index. If set to "min", file renaming and the counter will follow the Fixed
Window strategy.
-     * @param compressionLevelStr The compression level, 0 (less) through 9 (more); applies
only to ZIP files.
-     * @param config The Configuration.
-     * @return A DefaultRolloverStrategy.
+     * Perform the rollover.
+     * @param manager The RollingFileManager name for current active log file.
+     * @return A RolloverDescription.
+     * @throws SecurityException if an error occurs.
      */
-    @PluginFactory
-    public static DefaultRolloverStrategy createStrategy(
-            @PluginAttribute("max") final String max,
-            @PluginAttribute("min") final String min,
-            @PluginAttribute("fileIndex") final String fileIndex,
-            @PluginAttribute("compressionLevel") final String compressionLevelStr,
-            @PluginConfiguration final Configuration config) {
-        final boolean useMax = fileIndex == null ? true : fileIndex.equalsIgnoreCase("max");
-        int minIndex;
-        if (min != null) {
-            minIndex = Integer.parseInt(min);
-            if (minIndex < 1) {
-                LOGGER.error("Minimum window size too small. Limited to " + MIN_WINDOW_SIZE);
-                minIndex = MIN_WINDOW_SIZE;
+    @Override
+    public RolloverDescription rollover(final RollingFileManager manager) throws SecurityException
{
+        if (maxIndex >= 0) {
+            int fileIndex;
+
+            if ((fileIndex = purge(minIndex, maxIndex, manager)) < 0) {
+                return null;
             }
-        } else {
-            minIndex = MIN_WINDOW_SIZE;
-        }
-        int maxIndex;
-        if (max != null) {
-            maxIndex = Integer.parseInt(max);
-            if (maxIndex < minIndex) {
-                maxIndex = minIndex < DEFAULT_WINDOW_SIZE ? DEFAULT_WINDOW_SIZE : minIndex;
-                LOGGER.error("Maximum window size must be greater than the minimum windows
size. Set to " + maxIndex);
+
+            final StringBuilder buf = new StringBuilder();
+            manager.getPatternProcessor().formatFileName(subst, buf, fileIndex);
+            final String currentFileName = manager.getFileName();
+
+            String renameTo = buf.toString();
+            final String compressedName = renameTo;
+            Action compressAction = null;
+
+            if (renameTo.endsWith(".gz")) {
+                renameTo = renameTo.substring(0, renameTo.length() - 3);
+                compressAction = new GZCompressAction(new File(renameTo), new File(compressedName),
true);
+            } else if (renameTo.endsWith(".zip")) {
+                renameTo = renameTo.substring(0, renameTo.length() - 4);
+                compressAction = new ZipCompressAction(new File(renameTo), new File(compressedName),
true, 
+                        compressionLevel);
             }
-        } else {
-            maxIndex = DEFAULT_WINDOW_SIZE;
+
+            final FileRenameAction renameAction =
+                new FileRenameAction(new File(currentFileName), new File(renameTo), false);
+
+            return new RolloverDescriptionImpl(currentFileName, false, renameAction, compressAction);
         }
-        final int compressionLevel = Integers.parseInt(compressionLevelStr, Deflater.DEFAULT_COMPRESSION);
-        return new DefaultRolloverStrategy(minIndex, maxIndex, useMax, compressionLevel,
config.getStrSubstitutor());
+
+        return null;
+    }
+
+    @Override
+    public String toString() {
+        return "DefaultRolloverStrategy(min=" + minIndex + ", max=" + maxIndex + ")";
     }
 
 }



Mime
View raw message