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 2020/07/31 04:20:38 UTC

[logging-log4j2] branch master updated: LOG4J2-2883 - When When using DirectFileRolloverStrategy the file pattern was not being recalculated on size based rollover after a time based rollover had occurred.

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 3cdd1f3  LOG4J2-2883 - When When using DirectFileRolloverStrategy the file pattern was not being recalculated on size based rollover after a time based rollover had occurred.
3cdd1f3 is described below

commit 3cdd1f312c997794fb6c741806ddd7f0024e0dcd
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Thu Jul 30 21:20:22 2020 -0700

    LOG4J2-2883 - When When using DirectFileRolloverStrategy the file pattern was not being recalculated on size based rollover after a time based rollover had occurred.
---
 .../core/appender/rolling/PatternProcessor.java    |  5 +-
 .../RollingDirectSizeTimeNewDirectoryTest.java     | 85 ++++++++++++++++++++++
 .../log4j-rolling-size-time-new-directory.xml      | 42 +++++++++++
 src/changes/changes.xml                            |  4 +
 4 files changed, 134 insertions(+), 2 deletions(-)

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 e56fd59..42f77f5 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
@@ -232,6 +232,7 @@ public class PatternProcessor {
     public void updateTime() {
         if (nextFileTime != 0 || !isTimeBased) {
 			prevFileTime = nextFileTime;
+			currentFileTime = 0;
 		}
     }
 
@@ -285,8 +286,8 @@ public class PatternProcessor {
                                      final Object obj) {
         // LOG4J2-628: we deliberately use System time, not the log4j.Clock time
         // for creating the file name of rolled-over files.
-		LOGGER.debug("Formatting file name. useCurrentTime={}, currentFileTime={}, prevFileTime={}",
-			useCurrentTime, currentFileTime, prevFileTime);
+		LOGGER.debug("Formatting file name. useCurrentTime={}, currentFileTime={}, prevFileTime={}, nextFileTime={}",
+			useCurrentTime, currentFileTime, prevFileTime, nextFileTime);
 		final long time = useCurrentTime ? currentFileTime != 0 ? currentFileTime : System.currentTimeMillis() :
                 prevFileTime != 0 ? prevFileTime : System.currentTimeMillis();
         formatFileName(buf, new Date(time), obj);
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingDirectSizeTimeNewDirectoryTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingDirectSizeTimeNewDirectoryTest.java
new file mode 100644
index 0000000..a9d3ee5
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingDirectSizeTimeNewDirectoryTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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 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 java.io.File;
+import java.util.List;
+
+import static java.util.Arrays.asList;
+import static java.util.Collections.sort;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+/**
+ * Tests
+ */
+public class RollingDirectSizeTimeNewDirectoryTest {
+    private static final String CONFIG = "log4j-rolling-size-time-new-directory.xml";
+
+    private static final String DIR = "target/rolling-size-time-new-directory";
+
+    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(RollingDirectSizeTimeNewDirectoryTest.class.getName());
+    }
+
+
+    @Test
+    public void streamClosedError() throws Exception {
+        for (int i = 0; i < 1000; i++) {
+            logger.info("nHq6p9kgfvWfjzDRYbZp");
+        }
+        Thread.sleep(1500);
+        for (int i = 0; i < 1000; i++) {
+            logger.info("nHq6p9kgfvWfjzDRYbZp");
+        }
+
+        File tempDirectoryAsFile = new File(DIR);
+        File[] loggingFolders = tempDirectoryAsFile.listFiles();
+        assertNotNull(loggingFolders);
+        // Check if two folders were created
+        assertTrue("Not enough directories created", loggingFolders.length >= 2);
+        for (File dir : loggingFolders) {
+            File[] files = dir.listFiles();
+            assertTrue("No files in directory " + dir.toString(), files != null && files.length > 0);
+            if (files.length < 3) {
+                System.out.println("Only " + files.length + " files created in " + dir.toString());
+                for (File logFile : files) {
+                    System.out.println("File name: " + logFile.getName() + " size: " + logFile.length());
+                }
+                fail();
+            }
+
+        }
+    }
+}
diff --git a/log4j-core/src/test/resources/log4j-rolling-size-time-new-directory.xml b/log4j-core/src/test/resources/log4j-rolling-size-time-new-directory.xml
new file mode 100644
index 0000000..883bba0
--- /dev/null
+++ b/log4j-core/src/test/resources/log4j-rolling-size-time-new-directory.xml
@@ -0,0 +1,42 @@
+<?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="logDir">target/rolling-size-time-new-directory</Property>
+  </Properties>
+
+  <Appenders>
+    <RollingFile name="RollingFile" filePattern="${logDir}/%d{yyyy_MM_dd-mm-ss}/messages-$${date:yyyy_MM_dd_HH_mm_ss_SSS}.log">
+      <PatternLayout>
+        <Pattern>%d{MM-dd-yy-HH-mm-ss} %p %C{1.} [%t] %m%n</Pattern>
+      </PatternLayout>
+      <Policies>
+        <TimeBasedTriggeringPolicy />
+        <SizeBasedTriggeringPolicy size="5KB"/>
+      </Policies>
+    </RollingFile>
+  </Appenders>
+
+  <Loggers>
+    <Root level="info">
+      <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 e2d6918..ad89e8b 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -181,6 +181,10 @@
       </action>
     </release>
     <release version="2.14.0" date="2020-MM-DD" description="GA Release 2.14.0">
+      <action issue="LOG4J2-2883" dev="rgoers" type="fix">
+        When using DirectFileRolloverStrategy the file pattern was not being recalculated on
+        size based rollover after a time based rollover had occurred.
+      </action>
       <action issue="LOG4J2-2875" dev="rgoers" type="fix">
         Rollover was failing to create directories when using a DirectFileeRolloverStrategy.
       </action>