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 fc57d47 LOG4J2-2610 - Explicitly set file creation time
fc57d47 is described below
commit fc57d47fc8cbba882a996b6038c1c5c264b11ed2
Author: Ralph Goers <[email protected]>
AuthorDate: Sun Jun 2 15:04:56 2019 -0700
LOG4J2-2610 - Explicitly set file creation time
---
.../logging/log4j/core/appender/FileManager.java | 11 ++-
.../rolling/RollingAppenderTimeAndSizeTest.java | 94 +++++++++++-----------
log4j-core/src/test/resources/log4j-rolling3.xml | 1 +
src/changes/changes.xml | 3 +
4 files changed, 62 insertions(+), 47 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 45a64ea..4a5866d 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;
@@ -135,7 +137,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..b0235b4 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
@@ -16,19 +16,6 @@
*/
package org.apache.logging.log4j.core.appender.rolling;
-import static org.apache.logging.log4j.hamcrest.Descriptors.that;
-import static org.apache.logging.log4j.hamcrest.FileMatchers.hasName;
-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.assertThat;
-import static org.junit.Assert.assertTrue;
-
-import java.io.File;
-import java.util.Arrays;
-import java.util.Random;
-
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.junit.LoggerContextRule;
import org.junit.Before;
@@ -36,6 +23,18 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
+import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.attribute.FileTime;
+import java.util.Arrays;
+import java.util.Random;
+
+import static org.apache.logging.log4j.hamcrest.Descriptors.that;
+import static org.apache.logging.log4j.hamcrest.FileMatchers.hasName;
+import static org.hamcrest.Matchers.endsWith;
+import static org.hamcrest.Matchers.hasItemInArray;
+import static org.junit.Assert.*;
+
/**
*
*/
@@ -59,38 +58,41 @@ public class RollingAppenderTimeAndSizeTest {
@Test
public void testAppender() throws Exception {
- Random rand = new Random();
- for (int j=0; j < 100; ++j) {
- int count = rand.nextInt(100);
- for (int i = 0; i < count; ++i) {
- logger.debug("This is test message number " +
i);
- }
- Thread.sleep(rand.nextInt(50));
- }
- Thread.sleep(50);
- final File dir = new File(DIR);
- assertTrue("Directory not created", dir.exists() &&
dir.listFiles().length > 0);
- final File[] files = dir.listFiles();
- Arrays.sort(files);
- assertNotNull(files);
- assertThat(files,
hasItemInArray(that(hasName(that(endsWith(".log"))))));
- int found = 0;
- int fileCounter = 0;
- String previous = "";
- for (final File file: files) {
- final String actual = file.getName();
- StringBuilder padding = new StringBuilder();
- String length = Long.toString(file.length());
- for (int i = length.length(); i < 10; ++i) {
- padding.append(" ");
- }
- final String[] fileParts = actual.split("_|\\.");
- fileCounter = previous.equals(fileParts[1]) ?
++fileCounter : 1;
- previous = fileParts[1];
- assertEquals("Incorrect file name. Expected counter
value of " + fileCounter + " in " + actual,
- Integer.toString(fileCounter), fileParts[2]);
-
-
- }
+ 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(50);
+ for (int i = 0; i < count; ++i) {
+ logger.debug("This is test message number " + i);
+ }
+ Thread.sleep(rand.nextInt(50));
+ }
+ Thread.sleep(50);
+ final File dir = new File(DIR);
+ assertTrue("Directory not created", dir.exists() &&
dir.listFiles().length > 0);
+ final File[] files = dir.listFiles();
+ Arrays.sort(files);
+ assertNotNull(files);
+ assertThat(files,
hasItemInArray(that(hasName(that(endsWith(".log"))))));
+ int found = 0;
+ int fileCounter = 0;
+ String previous = "";
+ for (final File file : files) {
+ final String actual = file.getName();
+ StringBuilder padding = new StringBuilder();
+ String length = Long.toString(file.length());
+ for (int i = length.length(); i < 10; ++i) {
+ padding.append(" ");
+ }
+ final String[] fileParts = actual.split("_|\\.");
+ fileCounter = previous.equals(fileParts[1]) ? ++fileCounter : 1;
+ 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 478eb02..f44fc99 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -396,6 +396,9 @@
</action>
</release>
<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>