Skip to content

Commit

Permalink
Merge pull request #257 from jamezp/LOGMGR-255
Browse files Browse the repository at this point in the history
[LOGMGR-255] If append is set to true for a rotating file which is al…
  • Loading branch information
jamezp authored Jun 19, 2019
2 parents c808650 + a7cacac commit 0a95854
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,17 @@ 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);
}
} 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down

0 comments on commit 0a95854

Please sign in to comment.