This is an automated email from the ASF dual-hosted git repository.

rgoers pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/release-2.x by this push:
     new 5681650  LOG4J2-2602 - Update file time when size based triggering 
policy is used without a time-based triggering policy
5681650 is described below

commit 56816509933d1155759a08bd1d2ade31e22613e9
Author: Ralph Goers <[email protected]>
AuthorDate: Sun May 12 15:23:44 2019 -0700

    LOG4J2-2602 - Update file time when size based triggering policy is used 
without a time-based triggering policy
---
 .../appender/rolling/CronTriggeringPolicy.java     |  1 +
 .../core/appender/rolling/PatternProcessor.java    |  8 +-
 .../rolling/TimeBasedTriggeringPolicy.java         |  1 +
 .../rolling/RollingAppenderSizeWithTimeTest.java   | 98 ++++++++++++++++++++++
 .../resources/log4j-rolling-size-with-time.xml     | 43 ++++++++++
 src/changes/changes.xml                            |  3 +
 src/site/xdoc/manual/appenders.xml                 |  7 +-
 7 files changed, 157 insertions(+), 4 deletions(-)

diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/CronTriggeringPolicy.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/CronTriggeringPolicy.java
index 14997e1..b029707 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/CronTriggeringPolicy.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/CronTriggeringPolicy.java
@@ -70,6 +70,7 @@ public final class CronTriggeringPolicy extends 
AbstractTriggeringPolicy {
         
aManager.getPatternProcessor().setCurrentFileTime(lastRegularRoll.getTime());
         LOGGER.debug("LastRollForFile {}, LastRegularRole {}", 
lastRollForFile, lastRegularRoll);
         
aManager.getPatternProcessor().setPrevFileTime(lastRegularRoll.getTime());
+        aManager.getPatternProcessor().setTimeBased(true);
         if (checkOnStartup && lastRollForFile != null && lastRegularRoll != 
null &&
                 lastRollForFile.before(lastRegularRoll)) {
             lastRollDate = lastRollForFile;
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java
index 6d1fb7e..a5af4fd 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java
@@ -57,6 +57,8 @@ public class PatternProcessor {
     private long nextFileTime = 0;
     private long currentFileTime = 0;
 
+    private boolean isTimeBased = false;
+
     private RolloverFrequency frequency = null;
 
     private final String pattern;
@@ -106,6 +108,10 @@ public class PatternProcessor {
         this.currentFileTime = copy.currentFileTime;
     }
 
+    public void setTimeBased(boolean isTimeBased) {
+        this.isTimeBased = isTimeBased;
+    }
+
     public long getCurrentFileTime() {
         return currentFileTime;
     }
@@ -213,7 +219,7 @@ public class PatternProcessor {
     }
 
     public void updateTime() {
-       if (nextFileTime != 0) {
+       if (nextFileTime != 0 || !isTimeBased) {
                        prevFileTime = nextFileTime;
                }
     }
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/TimeBasedTriggeringPolicy.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/TimeBasedTriggeringPolicy.java
index fd5897a..f30cd7e 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/TimeBasedTriggeringPolicy.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/TimeBasedTriggeringPolicy.java
@@ -115,6 +115,7 @@ public final class TimeBasedTriggeringPolicy extends 
AbstractTriggeringPolicy {
 
         // LOG4J2-531: call getNextTime twice to force initialization of both 
prevFileTime and nextFileTime
         aManager.getPatternProcessor().getNextTime(current, interval, 
modulate);
+        aManager.getPatternProcessor().setTimeBased(true);
 
         nextRolloverMillis = ThreadLocalRandom.current().nextLong(0, 1 + 
maxRandomDelayMillis)
                 + aManager.getPatternProcessor().getNextTime(current, 
interval, modulate);
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeWithTimeTest.java
 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeWithTimeTest.java
new file mode 100644
index 0000000..2283aac
--- /dev/null
+++ 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeWithTimeTest.java
@@ -0,0 +1,98 @@
+/*
+ * 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 java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.commons.compress.utils.IOUtils;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.junit.LoggerContextRule;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.RuleChain;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+/**
+ * LOG4J2-2602.
+ */
+public class RollingAppenderSizeWithTimeTest {
+
+    private static final String CONFIG = "log4j-rolling-size-with-time.xml";
+
+    private static final String DIR = "target/rolling-size-test";
+
+    public static LoggerContextRule loggerContextRule = LoggerContextRule
+        .createShutdownTimeoutLoggerContextRule(CONFIG);
+
+    @Rule
+    public RuleChain chain = loggerContextRule.withCleanFoldersRule(DIR);
+
+    private Logger logger;
+
+    @Before
+    public void setUp() throws Exception {
+        this.logger = 
loggerContextRule.getLogger(RollingAppenderSizeWithTimeTest.class.getName());
+    }
+
+    @Test
+    public void testAppender() throws Exception {
+        final List<String> messages = new ArrayList<>();
+        for (int i = 0; i < 1000; ++i) {
+            final String message = "This is test message number " + i;
+            messages.add(message);
+            logger.debug(message);
+            if (i % 100 == 0) {
+                Thread.sleep(500);
+            }
+        }
+        if (!loggerContextRule.getLoggerContext().stop(30, TimeUnit.SECONDS)) {
+            System.err.println("Could not stop cleanly " + loggerContextRule + 
" for " + this);
+        }
+        final File dir = new File(DIR);
+        assertTrue("Directory not created", dir.exists());
+        final File[] files = dir.listFiles();
+        assertNotNull(files);
+        for (final File file : files) {
+            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            try (FileInputStream fis = new FileInputStream(file)) {
+                try {
+                    IOUtils.copy(fis, baos);
+                } catch (final Exception ex) {
+                    ex.printStackTrace();
+                    fail("Unable to read " + file.getAbsolutePath());
+                }
+            }
+            final String text = new String(baos.toByteArray(), 
Charset.defaultCharset());
+            final String[] lines = text.split("[\\r\\n]+");
+            for (final String line : lines) {
+                messages.remove(line);
+            }
+        }
+        assertTrue("Log messages lost : " + messages.size(), 
messages.isEmpty());
+        assertTrue("Files not rolled : " + files.length, files.length > 2);
+    }
+}
diff --git a/log4j-core/src/test/resources/log4j-rolling-size-with-time.xml 
b/log4j-core/src/test/resources/log4j-rolling-size-with-time.xml
new file mode 100644
index 0000000..b3dd0d9
--- /dev/null
+++ b/log4j-core/src/test/resources/log4j-rolling-size-with-time.xml
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+
+-->
+<Configuration status="WARN" name="XMLConfigTest">
+  <Properties>
+    <Property name="filename">target/rolling-size-test/rolling.log</Property>
+  </Properties>
+  <ThresholdFilter level="debug"/>
+
+  <Appenders>
+    <RollingFile name="RollingFile" fileName="${filename}"
+                 
filePattern="target/rolling-size-test/rollingtest-%d{yyyy-MM-DD'T'HH-mm-ss-SSS}.log">
+      <PatternLayout>
+        <Pattern>%m%n</Pattern>
+      </PatternLayout>
+      <SizeBasedTriggeringPolicy size="1000" />
+      <DefaultRolloverStrategy max="500"/>
+    </RollingFile>
+  </Appenders>
+
+  <Loggers>
+
+    <Root level="debug">
+      <AppenderRef ref="RollingFile"/>
+    </Root>
+  </Loggers>
+
+</Configuration>
\ No newline at end of file
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 9abc86d..7b620a8 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -30,6 +30,9 @@
          - "remove" - Removed
     -->
     <release version="2.12.0" date="2019-MM-DD" description="GA Release 
2.12.0">
+      <action issue="LOG4J2-2602" dev="rgoers" type="fix">
+        Update file time when size based triggering policy is used without a 
time-based triggering policy.
+      </action>
       <action issue="LOG4J2-2597" dev="rgoers" type="fix">
         Throw better exception message when both log4j-slf4j-impl and 
log4j-to-slf4j are present.
       </action>
diff --git a/src/site/xdoc/manual/appenders.xml 
b/src/site/xdoc/manual/appenders.xml
index 62cf5ee..6bc8280 100644
--- a/src/site/xdoc/manual/appenders.xml
+++ b/src/site/xdoc/manual/appenders.xml
@@ -3243,9 +3243,10 @@ public class JpaLogEntity extends 
AbstractLogEventWrapperEntity {
               <p>
                 The <code>SizeBasedTriggeringPolicy</code> causes a rollover 
once the file has reached the specified
                 size. The size can be specified in bytes, with the suffix KB, 
MB or GB, for example <code>20MB</code>.
-                The file pattern must contain a <code>%i</code> otherwise the 
target file will be overwritten on
-                every rollover as the SizeBased Triggering Policy does not 
cause the timestamp value
-                in the file name to change.
+                When combined with a time based triggering policy the file 
pattern must contain a <code>%i</code>
+                otherwise the target file will be overwritten on every 
rollover as the SizeBased Triggering Policy
+                will not cause the timestamp value in the file name to change. 
When used without a time based
+                triggering policy the SizeBased Triggering Policy will cause 
the timestamp value to change.
               </p>
             <h5>TimeBased Triggering Policy</h5>
               <p>

Reply via email to