You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2012/10/04 19:58:44 UTC

svn commit: r1394179 - in /logging/log4j/log4j2/trunk: core/src/main/java/org/apache/logging/log4j/core/appender/rolling/ core/src/test/resources/ src/changes/ src/site/xdoc/manual/

Author: rgoers
Date: Thu Oct  4 17:58:43 2012
New Revision: 1394179

URL: http://svn.apache.org/viewvc?rev=1394179&view=rev
Log:
LOG4J2-35 - Add interval and modulate attributes to TimeBasedTriggeringPolicy

Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/rolling/TimeBasedTriggeringPolicy.java
    logging/log4j/log4j2/trunk/core/src/test/resources/log4j-rolling2.xml
    logging/log4j/log4j2/trunk/src/changes/changes.xml
    logging/log4j/log4j2/trunk/src/site/xdoc/manual/appenders.xml

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java?rev=1394179&r1=1394178&r2=1394179&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java (original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java Thu Oct  4 17:58:43 2012
@@ -71,21 +71,12 @@ public class PatternProcessor {
     }
 
     /**
-     * Return the next expire time.
-     * @param current The current time.
-     * @return The next expire time.
-     */
-    public long getNextTime(long current) {
-        return getNextTime(current, 1);
-    }
-
-    /**
      * Return the next potential rollover time.
      * @param current The current time.
      * @param increment The increment to the next time.
      * @return the next potential rollover time.
      */
-    public long getNextTime(long current, int increment) {
+    public long getNextTime(long current, int increment, boolean modulus) {
         if (frequency == null) {
             throw new IllegalStateException("Pattern does not contain a date");
         }
@@ -95,41 +86,46 @@ public class PatternProcessor {
         cal.set(currentCal.get(Calendar.YEAR), 0, 1, 0, 0, 0);
         cal.set(Calendar.MILLISECOND, 0);
         if (frequency == RolloverFrequency.ANNUALLY) {
-            cal.add(Calendar.YEAR, increment);
+            increment(cal, Calendar.YEAR, increment, modulus);
             return cal.getTimeInMillis();
         }
         if (frequency == RolloverFrequency.MONTHLY) {
-            cal.add(Calendar.MONTH, increment);
+            increment(cal, Calendar.MONTH, increment, modulus);
             return cal.getTimeInMillis();
         }
         if (frequency == RolloverFrequency.WEEKLY) {
-            cal.set(Calendar.WEEK_OF_YEAR, currentCal.get(Calendar.WEEK_OF_YEAR) + increment);
+            increment(cal, Calendar.WEEK_OF_YEAR, increment, modulus);
             return cal.getTimeInMillis();
         }
         cal.set(Calendar.DAY_OF_YEAR, currentCal.get(Calendar.DAY_OF_YEAR));
         if (frequency == RolloverFrequency.DAILY) {
-            cal.add(Calendar.DAY_OF_YEAR, increment);
+            increment(cal, Calendar.DAY_OF_YEAR, increment, modulus);
             return cal.getTimeInMillis();
         }
         cal.set(Calendar.HOUR, currentCal.get(Calendar.HOUR));
         if (frequency == RolloverFrequency.HOURLY) {
-            cal.add(Calendar.HOUR, increment);
+            increment(cal, Calendar.HOUR, increment, modulus);
             return cal.getTimeInMillis();
         }
         cal.set(Calendar.MINUTE, currentCal.get(Calendar.MINUTE));
         if (frequency == RolloverFrequency.EVERY_MINUTE) {
-            cal.add(Calendar.MINUTE, increment);
+            increment(cal, Calendar.MINUTE, increment, modulus);
             return cal.getTimeInMillis();
         }
         cal.set(Calendar.SECOND, currentCal.get(Calendar.SECOND));
         if (frequency == RolloverFrequency.EVERY_SECOND) {
-            cal.add(Calendar.SECOND, increment);
+            increment(cal, Calendar.SECOND, increment, modulus);
             return cal.getTimeInMillis();
         }
-        cal.set(Calendar.MILLISECOND, currentCal.get(Calendar.MILLISECOND) + increment);
+        increment(cal, Calendar.MILLISECOND, increment, modulus);
         return cal.getTimeInMillis();
     }
 
+    private void increment(Calendar cal, int type, int increment, boolean modulate) {
+        int interval =  modulate ? increment - (cal.get(type) % increment) : increment;
+        cal.add(type, interval);
+    }
+
     /**
      * Format file name.
      *

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/rolling/TimeBasedTriggeringPolicy.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/rolling/TimeBasedTriggeringPolicy.java?rev=1394179&r1=1394178&r2=1394179&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/rolling/TimeBasedTriggeringPolicy.java (original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/rolling/TimeBasedTriggeringPolicy.java Thu Oct  4 17:58:43 2012
@@ -18,6 +18,7 @@ package org.apache.logging.log4j.core.ap
 
 import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginAttr;
 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
 
 /**
@@ -27,10 +28,14 @@ import org.apache.logging.log4j.core.con
 public final class TimeBasedTriggeringPolicy implements TriggeringPolicy {
 
     private long nextRollover;
+    private final int interval;
+    private final boolean modulate;
 
     private RollingFileManager manager;
 
-    private TimeBasedTriggeringPolicy() {
+    private TimeBasedTriggeringPolicy(int interval, boolean modulate) {
+        this.interval = interval;
+        this.modulate = modulate;
     }
 
     /**
@@ -39,7 +44,7 @@ public final class TimeBasedTriggeringPo
      */
     public void initialize(RollingFileManager manager) {
         this.manager = manager;
-        nextRollover = manager.getProcessor().getNextTime(manager.getFileTime());
+        nextRollover = manager.getProcessor().getNextTime(manager.getFileTime(), interval, modulate);
     }
 
     /**
@@ -53,7 +58,7 @@ public final class TimeBasedTriggeringPo
         }
         long now = System.currentTimeMillis();
         if (now > nextRollover) {
-            nextRollover = manager.getProcessor().getNextTime(now);
+            nextRollover = manager.getProcessor().getNextTime(now, interval, modulate);
             return true;
         }
         return false;
@@ -69,7 +74,10 @@ public final class TimeBasedTriggeringPo
      * @return a TimeBasedTriggeringPolicy.
      */
     @PluginFactory
-    public static TimeBasedTriggeringPolicy createPolicy() {
-        return new TimeBasedTriggeringPolicy();
+    public static TimeBasedTriggeringPolicy createPolicy(@PluginAttr("interval") String interval,
+                                                         @PluginAttr("modulate") String modulate) {
+        int increment = interval == null ? 1 : Integer.parseInt(interval);
+        boolean mod = modulate == null ? false : Boolean.parseBoolean(modulate);
+        return new TimeBasedTriggeringPolicy(increment, mod);
     }
 }

Modified: logging/log4j/log4j2/trunk/core/src/test/resources/log4j-rolling2.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/resources/log4j-rolling2.xml?rev=1394179&r1=1394178&r2=1394179&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/resources/log4j-rolling2.xml (original)
+++ logging/log4j/log4j2/trunk/core/src/test/resources/log4j-rolling2.xml Thu Oct  4 17:58:43 2012
@@ -32,7 +32,7 @@
       <PatternLayout>
         <pattern>%d %p %C{1.} [%t] %m%n</pattern>
       </PatternLayout>
-      <TimeBasedTriggeringPolicy />
+      <TimeBasedTriggeringPolicy interval="2" modulate="true"/>
     </RollingFile>
     <List name="List">
       <filters>

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1394179&r1=1394178&r2=1394179&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Thu Oct  4 17:58:43 2012
@@ -23,6 +23,10 @@
 
   <body>
     <release version="2.0-beta2" date="TBD" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-35" dev="rgoers" type="add">
+        Add interval and modulate options to TimeBasedTriggeringPolicy to allow more fine-grained control of
+        when file rolling should occur.
+      </action>
       <action issue="LOG4J2-58" dev="rgoers" type="add">
         Add support for filtering packages from stack traces.
       </action>

Modified: logging/log4j/log4j2/trunk/src/site/xdoc/manual/appenders.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/site/xdoc/manual/appenders.xml?rev=1394179&r1=1394178&r2=1394179&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/site/xdoc/manual/appenders.xml (original)
+++ logging/log4j/log4j2/trunk/src/site/xdoc/manual/appenders.xml Thu Oct  4 17:58:43 2012
@@ -1033,8 +1033,33 @@
             <h5>TimeBased Triggering Policy</h5>
               <p>
                 Causes a rollover once the date/time pattern no longer applies to the active file. This policy
-                takes no parameters.
+                accepts an "increment" attribute which indicates how frequently the rollover should occur based on the
+                time pattern and a "modulate" boolean attribute.
               </p>
+                <table border="1" width="100%">
+                  <tr>
+                    <th>Parameter Name</th>
+                    <th>Type</th>
+                    <th>Description</th>
+                  </tr>
+                  <tr>
+                    <td>interval</td>
+                    <td>integer</td>
+                    <td>How often a rollover should occur based on the most specific time unit in the date pattern.
+                      For example, with a date pattern with hours as the most specific item and and increment of 4 rollovers
+                      would occur every 4 hours.
+                      The default value is 1.</td>
+                  </tr>
+                  <tr>
+                    <td>modulate</td>
+                    <td>boolean</td>
+                    <td>Indicates whether the interval should be adjusted to cause the next rollover to occur on
+                      the interval boundary. For example, if the item is hours, the current hour is 3 am and the
+                      interval is 4 then then the first rollover will occur at 4 am and then next ones will occur at
+                      8 am, noon, 4pm, etc.</td>
+                  </tr>
+                  <caption align="top">TimeBasedTriggeringPolicy Parameters</caption>
+                </table>
           <a name="RolloverStrategies"/>
           <h4>Rollover Strategies</h4>
             <a name="DefaultRolloverStrategy"/>
@@ -1124,6 +1149,34 @@
 </configuration>
   ]]></source>
         </p>
+          <p>
+            Below is a sample configuration that uses a RollingFileAppender with both the time and size based
+            triggering policies, will create up to 7 archives on the same day (1-7) that are stored in a directory
+            based on the current year and month, and will compress each
+            archive using gzip and will roll every 6 hours when the hour is divisible by 6:
+
+            <source><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
+<configuration status="warn" name="MyApp" packages="">
+  <appenders>
+    <RollingFile name="RollingFile" fileName="logs/app.log"
+                                    filePattern="logs/$${date:yyyy-MM}/app-%d{yyyy-MM-dd-HH}-%i.log.gz">
+      <PatternLayout>
+        <pattern>%d %p %C{1.} [%t] %m%n</pattern>
+      </PatternLayout>
+      <Policies>
+        <TimeBasedTriggeringPolicy interval="6" modulate="true"/>
+        <SizeBasedTriggeringPolicy size="250 MB"/>
+      </Policies>
+    </RollingFile>
+  </appenders>
+  <loggers>
+    <root level="error">
+      <appender-ref ref="RollingFile"/>
+    </root>
+  </loggers>
+</configuration>
+  ]]></source>
+          </p>
         </subsection>
         <a name="RoutingAppender"/>
         <subsection name="RoutingAppender">