From a7cacacdad3170d517568d47f2d9e1a7069c40cc Mon Sep 17 00:00:00 2001 From: James Perkins Date: Wed, 19 Jun 2019 14:12:10 -0700 Subject: [PATCH] [LOGMGR-255] If append is set to true for a rotating file which is also set to be archived delete the file after it's been archived. This ensures the base file does not continue to be appended to after it's been rotated. --- .../java/org/jboss/logmanager/ext/handlers/SuffixRotator.java | 4 ++++ .../ext/handlers/PeriodicRotatingFileHandlerTests.java | 4 ++++ .../ext/handlers/PeriodicSizeRotatingFileHandlerTests.java | 2 ++ .../logmanager/ext/handlers/SizeRotatingFileHandlerTests.java | 2 ++ 4 files changed, 12 insertions(+) diff --git a/ext/src/main/java/org/jboss/logmanager/ext/handlers/SuffixRotator.java b/ext/src/main/java/org/jboss/logmanager/ext/handlers/SuffixRotator.java index e15ae441..9f77ce86 100644 --- a/ext/src/main/java/org/jboss/logmanager/ext/handlers/SuffixRotator.java +++ b/ext/src/main/java/org/jboss/logmanager/ext/handlers/SuffixRotator.java @@ -155,6 +155,8 @@ void rotate(final ErrorManager errorManager, final Path source, final String suf if (compressionType == CompressionType.GZIP) { try { archiveGzip(source, target); + // Delete the file after it's archived to behave like a file move or rename + Files.delete(source); } catch (Exception e) { errorManager.error(String.format("Failed to compress %s to %s. Compressed file may be left on the " + "filesystem corrupted.", source, target), e, ErrorManager.WRITE_FAILURE); @@ -162,6 +164,8 @@ void rotate(final ErrorManager errorManager, final Path source, final String suf } else if (compressionType == CompressionType.ZIP) { try { archiveZip(source, target); + // Delete the file after it's archived to behave like a file move or rename + Files.delete(source); } catch (Exception e) { errorManager.error(String.format("Failed to compress %s to %s. Compressed file may be left on the " + "filesystem corrupted.", source, target), e, ErrorManager.WRITE_FAILURE); diff --git a/ext/src/test/java/org/jboss/logmanager/ext/handlers/PeriodicRotatingFileHandlerTests.java b/ext/src/test/java/org/jboss/logmanager/ext/handlers/PeriodicRotatingFileHandlerTests.java index dad2dcc3..1f6e500d 100644 --- a/ext/src/test/java/org/jboss/logmanager/ext/handlers/PeriodicRotatingFileHandlerTests.java +++ b/ext/src/test/java/org/jboss/logmanager/ext/handlers/PeriodicRotatingFileHandlerTests.java @@ -57,6 +57,8 @@ public void createHandler() throws FileNotFoundException { // Create the handler handler = new PeriodicRotatingFileHandler(logFile.toFile(), rotateFormatter.toPattern(), false); handler.setFormatter(FORMATTER); + // Set append to true to ensure the rotated file is overwritten + handler.setAppend(true); } @After @@ -108,6 +110,8 @@ public void testFailedRotate() throws Exception { final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); final int currentDay = cal.get(Calendar.DAY_OF_MONTH); final int nextDay = currentDay + 1; + // Set to false for this specific test + handler.setAppend(false); final String currentDate = sdf.format(cal.getTime()); diff --git a/ext/src/test/java/org/jboss/logmanager/ext/handlers/PeriodicSizeRotatingFileHandlerTests.java b/ext/src/test/java/org/jboss/logmanager/ext/handlers/PeriodicSizeRotatingFileHandlerTests.java index 73842caf..a535804d 100644 --- a/ext/src/test/java/org/jboss/logmanager/ext/handlers/PeriodicSizeRotatingFileHandlerTests.java +++ b/ext/src/test/java/org/jboss/logmanager/ext/handlers/PeriodicSizeRotatingFileHandlerTests.java @@ -248,6 +248,8 @@ private void testArchiveRotate(final String dateSuffix, final String archiveSuff handler.setRotateOnBoot(rotateOnBoot); handler.setFile(logFile); handler.setSuffix((dateSuffix == null ? "" : dateSuffix) + archiveSuffix); + // Set append to true to ensure the rotated file is overwritten + handler.setAppend(true); // Allow a few rotates for (int i = 0; i < 100; i++) { diff --git a/ext/src/test/java/org/jboss/logmanager/ext/handlers/SizeRotatingFileHandlerTests.java b/ext/src/test/java/org/jboss/logmanager/ext/handlers/SizeRotatingFileHandlerTests.java index 66b4699e..075dca9d 100644 --- a/ext/src/test/java/org/jboss/logmanager/ext/handlers/SizeRotatingFileHandlerTests.java +++ b/ext/src/test/java/org/jboss/logmanager/ext/handlers/SizeRotatingFileHandlerTests.java @@ -211,6 +211,8 @@ private void testArchiveRotate(final String archiveSuffix, final boolean rotateO handler.setRotateOnBoot(rotateOnBoot); handler.setFile(logFile); handler.setSuffix(archiveSuffix); + // Set append to true to ensure the rotated file is overwritten + handler.setAppend(true); // Allow a few rotates for (int i = 0; i < 100; i++) {