logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rpo...@apache.org
Subject svn commit: r1563132 - in /logging/log4j/log4j2/trunk: log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/ log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/ src/changes/
Date Fri, 31 Jan 2014 13:28:48 GMT
Author: rpopma
Date: Fri Jan 31 13:28:47 2014
New Revision: 1563132

URL: http://svn.apache.org/r1563132
Log:
LOG4J2-385 Fixed issues with time-based file rollover (monthly, weekly, hourly and every minute)

Added:
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessorTest.java
  (with props)
Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java?rev=1563132&r1=1563131&r2=1563132&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java
Fri Jan 31 13:28:47 2014
@@ -102,6 +102,7 @@ public class PatternProcessor {
             nextFileTime = cal.getTimeInMillis();
             return nextTime;
         }
+        cal.set(Calendar.MONTH, currentCal.get(Calendar.MONTH));
         if (frequency == RolloverFrequency.MONTHLY) {
             increment(cal, Calendar.MONTH, increment, modulus);
             nextTime = cal.getTimeInMillis();
@@ -110,7 +111,9 @@ public class PatternProcessor {
             return nextTime;
         }
         if (frequency == RolloverFrequency.WEEKLY) {
+            cal.set(Calendar.WEEK_OF_YEAR, currentCal.get(Calendar.WEEK_OF_YEAR));
             increment(cal, Calendar.WEEK_OF_YEAR, increment, modulus);
+            cal.set(Calendar.DAY_OF_WEEK, currentCal.getFirstDayOfWeek());
             nextTime = cal.getTimeInMillis();
             cal.add(Calendar.WEEK_OF_YEAR, -1);
             nextFileTime = cal.getTimeInMillis();
@@ -124,11 +127,11 @@ public class PatternProcessor {
             nextFileTime = cal.getTimeInMillis();
             return nextTime;
         }
-        cal.set(Calendar.HOUR, currentCal.get(Calendar.HOUR));
+        cal.set(Calendar.HOUR_OF_DAY, currentCal.get(Calendar.HOUR_OF_DAY));
         if (frequency == RolloverFrequency.HOURLY) {
-            increment(cal, Calendar.HOUR, increment, modulus);
+            increment(cal, Calendar.HOUR_OF_DAY, increment, modulus);
             nextTime = cal.getTimeInMillis();
-            cal.add(Calendar.HOUR, -1);
+            cal.add(Calendar.HOUR_OF_DAY, -1);
             nextFileTime = cal.getTimeInMillis();
             return nextTime;
         }
@@ -148,6 +151,7 @@ public class PatternProcessor {
             nextFileTime = cal.getTimeInMillis();
             return nextTime;
         }
+        cal.set(Calendar.MILLISECOND, currentCal.get(Calendar.MILLISECOND));
         increment(cal, Calendar.MILLISECOND, increment, modulus);
         nextTime = cal.getTimeInMillis();
         cal.add(Calendar.MILLISECOND, -1);

Added: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessorTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessorTest.java?rev=1563132&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessorTest.java
(added)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessorTest.java
Fri Jan 31 13:28:47 2014
@@ -0,0 +1,198 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.logging.log4j.core.appender.rolling;
+
+import static org.junit.Assert.*;
+
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Locale;
+
+import org.junit.Test;
+
+/**
+ * Tests the PatternProcessor class.
+ */
+public class PatternProcessorTest {
+
+    private String format(long time) {
+        String actualStr = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS").format(new Date(time));
+        return actualStr;
+    }
+
+    @Test
+    public void testGetNextTimeMonthlyReturnsFirstDayOfNextMonth() {
+        PatternProcessor pp = new PatternProcessor("logs/app-%d{yyyy-MM}.log.gz");
+        Calendar initial = Calendar.getInstance();
+        initial.set(2014, 9, 15, 10, 31, 59); // Oct 15th
+        long actual = pp.getNextTime(initial.getTimeInMillis(), 1, false);
+
+        // We expect 1st day of next month
+        Calendar expected = Calendar.getInstance();
+        expected.set(2014, 10, 1, 0, 0, 00);
+        expected.set(Calendar.MILLISECOND, 0);
+        assertEquals(format(expected.getTimeInMillis()), format(actual));
+    }
+
+    @Test
+    public void testGetNextTimeMonthlyReturnsFirstDayOfNextMonth2() {
+        PatternProcessor pp = new PatternProcessor("logs/app-%d{yyyy-MM}.log.gz");
+        Calendar initial = Calendar.getInstance();
+        initial.set(2014, 0, 31, 10, 31, 59); // 2014 Jan 31st
+        long actual = pp.getNextTime(initial.getTimeInMillis(), 1, false);
+
+        // Expect 1st of next month: 2014 Feb 1st
+        Calendar expected = Calendar.getInstance();
+        expected.set(2014, 1, 1, 0, 0, 00);
+        expected.set(Calendar.MILLISECOND, 0);
+        assertEquals(format(expected.getTimeInMillis()), format(actual));
+    }
+
+    @Test
+    public void testGetNextTimeMonthlyReturnsFirstDayOfNextMonth3() {
+        PatternProcessor pp = new PatternProcessor("logs/app-%d{yyyy-MM}.log.gz");
+        Calendar initial = Calendar.getInstance();
+        initial.set(2014, 11, 31, 10, 31, 59); // 2014 Dec 31st
+        long actual = pp.getNextTime(initial.getTimeInMillis(), 1, false);
+
+        // Expect 1st of next month: 2015 Jan 1st
+        Calendar expected = Calendar.getInstance();
+        expected.set(2015, 0, 1, 0, 0, 00);
+        expected.set(Calendar.MILLISECOND, 0);
+        assertEquals(format(expected.getTimeInMillis()), format(actual));
+    }
+
+    @Test
+    public void testGetNextTimeWeeklyReturnsFirstDayOfNextWeek_US() {
+        Locale old = Locale.getDefault();
+        Locale.setDefault(Locale.US); // force 1st day of the week to be Sunday
+
+        try {
+            PatternProcessor pp = new PatternProcessor("logs/app-%d{yyyy-MM-W}.log.gz");
+            Calendar initial = Calendar.getInstance();
+            initial.set(2014, 2, 4, 10, 31, 59); // Tue, March 4, 2014
+            long actual = pp.getNextTime(initial.getTimeInMillis(), 1, false);
+
+            // expect Sunday, March 9, 2014
+            Calendar expected = Calendar.getInstance();
+            expected.set(2014, 2, 9, 0, 0, 00);
+            expected.set(Calendar.MILLISECOND, 0);
+            assertEquals(format(expected.getTimeInMillis()), format(actual));
+        } finally {
+            Locale.setDefault(old);
+        }
+    }
+
+    @Test
+    public void testGetNextTimeWeeklyReturnsFirstDayOfNextWeek_FRANCE() {
+        Locale old = Locale.getDefault();
+        Locale.setDefault(Locale.FRANCE); // force 1st day of the week to be Monday
+
+        try {
+            PatternProcessor pp = new PatternProcessor("logs/app-%d{yyyy-MM-W}.log.gz");
+            Calendar initial = Calendar.getInstance();
+            initial.set(2014, 2, 4, 10, 31, 59); // Tue, March 4, 2014
+            long actual = pp.getNextTime(initial.getTimeInMillis(), 1, false);
+
+            // expect Monday, March 10, 2014
+            Calendar expected = Calendar.getInstance();
+            expected.set(2014, 2, 10, 0, 0, 00);
+            expected.set(Calendar.MILLISECOND, 0);
+            assertEquals(format(expected.getTimeInMillis()), format(actual));
+        } finally {
+            Locale.setDefault(old);
+        }
+    }
+
+    @Test
+    public void testGetNextTimeHourlyReturnsFirstMinuteOfNextHour() {
+        PatternProcessor pp = new PatternProcessor("logs/app-%d{yyyy-MM-dd-HH}.log.gz");
+        Calendar initial = Calendar.getInstance();
+        initial.set(2014, 2, 4, 10, 31, 59); // Tue, March 4, 2014, 10:31
+        long actual = pp.getNextTime(initial.getTimeInMillis(), 1, false);
+
+        // expect Wed, March 4, 2014, 11:00
+        Calendar expected = Calendar.getInstance();
+        expected.set(2014, 2, 4, 11, 00, 00);
+        expected.set(Calendar.MILLISECOND, 0);
+        assertEquals(format(expected.getTimeInMillis()), format(actual));
+    }
+
+    @Test
+    public void testGetNextTimeHourlyReturnsFirstMinuteOfNextHour2() {
+        PatternProcessor pp = new PatternProcessor("logs/app-%d{yyyy-MM-dd-HH}.log.gz");
+        Calendar initial = Calendar.getInstance();
+        initial.set(2014, 2, 4, 23, 31, 59); // Tue, March 4, 2014, 23:31
+        long actual = pp.getNextTime(initial.getTimeInMillis(), 1, false);
+
+        // expect Wed, March 5, 2014, 00:00
+        Calendar expected = Calendar.getInstance();
+        expected.set(2014, 2, 5, 00, 00, 00);
+        expected.set(Calendar.MILLISECOND, 0);
+        assertEquals(format(expected.getTimeInMillis()), format(actual));
+    }
+
+    @Test
+    public void testGetNextTimeMinutelyReturnsFirstSecondOfNextMinute() {
+        PatternProcessor pp = new PatternProcessor("logs/app-%d{yyyy-MM-dd-HH-mm}.log.gz");
+        Calendar initial = Calendar.getInstance();
+        initial.set(2014, 2, 4, 10, 31, 59); // Tue, March 4, 2014, 10:31
+        initial.set(Calendar.MILLISECOND, 0);
+        assertEquals("2014/03/04 10:31:59.000", format(initial.getTimeInMillis()));
+        long actual = pp.getNextTime(initial.getTimeInMillis(), 1, false);
+
+        // expect Tue, March 4, 2014, 10:32
+        Calendar expected = Calendar.getInstance();
+        expected.set(2014, 2, 4, 10, 32, 00);
+        expected.set(Calendar.MILLISECOND, 0);
+        assertEquals(format(expected.getTimeInMillis()), format(actual));
+    }
+
+    @Test
+    public void testGetNextTimeSecondlyReturnsFirstMillisecOfNextSecond() {
+        PatternProcessor pp = new PatternProcessor("logs/app-%d{yyyy-MM-dd-HH-mm-ss}.log.gz");
+        Calendar initial = Calendar.getInstance();
+        initial.set(2014, 2, 4, 10, 31, 53); // Tue, March 4, 2014, 10:31:53
+        initial.set(Calendar.MILLISECOND, 123);
+        assertEquals("2014/03/04 10:31:53.123", format(initial.getTimeInMillis()));
+        long actual = pp.getNextTime(initial.getTimeInMillis(), 1, false);
+
+        // expect Tue, March 4, 2014, 10:31:54
+        Calendar expected = Calendar.getInstance();
+        expected.set(2014, 2, 4, 10, 31, 54);
+        expected.set(Calendar.MILLISECOND, 0);
+        assertEquals(format(expected.getTimeInMillis()), format(actual));
+    }
+
+    @Test
+    public void testGetNextTimeMillisecondlyReturnsNextMillisec() {
+        PatternProcessor pp = new PatternProcessor("logs/app-%d{yyyy-MM-dd-HH-mm-ss.SSS}.log.gz");
+        Calendar initial = Calendar.getInstance();
+        initial.set(2014, 2, 4, 10, 31, 53); // Tue, March 4, 2014, 10:31:53.123
+        initial.set(Calendar.MILLISECOND, 123);
+        assertEquals("2014/03/04 10:31:53.123", format(initial.getTimeInMillis()));
+        long actual = pp.getNextTime(initial.getTimeInMillis(), 1, false);
+
+        // expect Tue, March 4, 2014, 10:31:53.124
+        Calendar expected = Calendar.getInstance();
+        expected.set(2014, 2, 4, 10, 31, 53);
+        expected.set(Calendar.MILLISECOND, 124);
+        assertEquals(format(expected.getTimeInMillis()), format(actual));
+    }
+}

Propchange: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1563132&r1=1563131&r2=1563132&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Fri Jan 31 13:28:47 2014
@@ -21,6 +21,9 @@
   </properties>
   <body>
     <release version="2.0-RC1" date="2014-MM-DD" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-385" dev="rpopma" type="fix" due-to="Ace Funk, Porfirio Partida">
+        Fixed issues with time-based file rollover (monthly, weekly, hourly and every minute).
+      </action>
       <action issue="LOG4J2-452" dev="nickwilliams" type="fix">
         Added a ServletContext attribute that, when set to "true", disables Log4j's auto-initialization
in
         Servlet 3.0+ web applications.



Mime
View raw message