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 2019/06/02 20:07:39 UTC

[logging-log4j2] branch release-2.x updated: LOG4J2-2610 - Explicitly set file creation time

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 6996fb0  LOG4J2-2610 - Explicitly set file creation time
6996fb0 is described below

commit 6996fb0433bec6f4e5fd528159a63a7c04d4fb7f
Author: Ralph Goers <Ra...@dslextreme.com>
AuthorDate: Sun Jun 2 14:05:34 2019 -0600

    LOG4J2-2610 - Explicitly set file creation time
---
 .../org/apache/logging/log4j/core/appender/FileManager.java  | 11 ++++++++++-
 .../appender/rolling/RollingAppenderTimeAndSizeTest.java     | 12 +++++++++---
 log4j-core/src/test/resources/log4j-rolling3.xml             |  1 +
 src/changes/changes.xml                                      |  3 +++
 4 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java
index 0906534..3596673 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java
@@ -25,9 +25,11 @@ import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
 import java.nio.channels.FileLock;
 import java.nio.file.FileSystems;
+import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.nio.file.attribute.FileOwnerAttributeView;
+import java.nio.file.attribute.FileTime;
 import java.nio.file.attribute.PosixFileAttributeView;
 import java.nio.file.attribute.PosixFilePermission;
 import java.nio.file.attribute.PosixFilePermissions;
@@ -185,7 +187,14 @@ public class FileManager extends OutputStreamManager {
     protected OutputStream createOutputStream() throws IOException {
         final String filename = getFileName();
         LOGGER.debug("Now writing to {} at {}", filename, new Date());
-        final FileOutputStream fos = new FileOutputStream(filename, isAppend);
+        final File file = new File(filename);
+        final FileOutputStream fos = new FileOutputStream(file, isAppend);
+        try {
+            FileTime now = FileTime.fromMillis(System.currentTimeMillis());
+            Files.setAttribute(file.toPath(), "creationTime", now);
+        } catch (Exception ex) {
+            LOGGER.warn("Unable to set current file tiem for {}", filename);
+        }
         defineAttributeView(Paths.get(filename));
         return fos;
     }
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeAndSizeTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeAndSizeTest.java
index a94300b..bac1b1e 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeAndSizeTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeAndSizeTest.java
@@ -22,10 +22,13 @@ import static org.hamcrest.Matchers.endsWith;
 import static org.hamcrest.Matchers.hasItemInArray;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
 import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.attribute.FileTime;
 import java.util.Arrays;
 import java.util.Random;
 
@@ -60,8 +63,11 @@ public class RollingAppenderTimeAndSizeTest {
     @Test
     public void testAppender() throws Exception {
 		Random rand = new Random();
+		final File logFile = new File("target/rolling3/rollingtest.log");
+		assertTrue("target/rolling3/rollingtest.log does not exist", logFile.exists());
+		FileTime time = (FileTime) Files.getAttribute(logFile.toPath(), "creationTime");
 		for (int j=0; j < 100; ++j) {
-			int count = rand.nextInt(100);
+			int count = rand.nextInt(50);
 			for (int i = 0; i < count; ++i) {
 				logger.debug("This is test message number " + i);
 			}
@@ -89,8 +95,8 @@ public class RollingAppenderTimeAndSizeTest {
 			previous = fileParts[1];
 			assertEquals("Incorrect file name. Expected counter value of " + fileCounter + " in " + actual,
 				Integer.toString(fileCounter), fileParts[2]);
-
-
 		}
+		FileTime endTime = (FileTime) Files.getAttribute(logFile.toPath(), "creationTime");
+		assertNotEquals("Creation times are equal", time, endTime);
     }
 }
diff --git a/log4j-core/src/test/resources/log4j-rolling3.xml b/log4j-core/src/test/resources/log4j-rolling3.xml
index a2c1310..56f6058 100644
--- a/log4j-core/src/test/resources/log4j-rolling3.xml
+++ b/log4j-core/src/test/resources/log4j-rolling3.xml
@@ -32,6 +32,7 @@
         <TimeBasedTriggeringPolicy />
         <SizeBasedTriggeringPolicy size="500" />
       </Policies>
+      <DefaultRolloverStrategy max="9"/>
     </RollingFile>
     <List name="List">
       <ThresholdFilter level="error"/>
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 2c042d7..fa9b13f 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-2610" dev="rgoers" type="fix">
+        Explicitly set file creation time.
+      </action>
       <action issue="LOG4J2-2561" dev="rgoers" type="fix" due-to="Ulrich Enslin">
         JEP223 version detection fix for JDK 9 and up.
       </action>