-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from jamezp/LOGMGR-154
[LOGMGR-154] Allow logging to the handler to continue even if the fil…
- Loading branch information
Showing
8 changed files
with
213 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,16 +30,20 @@ | |
import java.util.Calendar; | ||
import java.util.List; | ||
|
||
import org.jboss.byteman.contrib.bmunit.BMRule; | ||
import org.jboss.byteman.contrib.bmunit.BMUnitRunner; | ||
import org.jboss.logmanager.ExtLogRecord; | ||
import org.jboss.logmanager.Level; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">James R. Perkins</a> | ||
*/ | ||
@RunWith(BMUnitRunner.class) | ||
public class PeriodicRotatingFileHandlerTests extends AbstractHandlerTest { | ||
private final static String FILENAME = "periodic-rotating-file-handler.log"; | ||
|
||
|
@@ -91,6 +95,50 @@ public void testArchiveRotateZip() throws Exception { | |
testArchiveRotate(".zip"); | ||
} | ||
|
||
@Test | ||
@BMRule(name = "Test failed rotated", | ||
targetClass = "java.nio.file.Files", | ||
targetMethod = "move", | ||
targetLocation = "AT ENTRY", | ||
condition = "$2.getFileName().toString().matches(\"periodic-rotating-file-handler\\.log\\.\\d+\")", | ||
action = "throw new IOException(\"Fail on purpose\")") | ||
public void testFailedRotate() throws Exception { | ||
final Calendar cal = Calendar.getInstance(); | ||
final Path rotatedFile = BASE_LOG_DIR.toPath().resolve(FILENAME + rotateFormatter.format(cal.getTime())); | ||
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | ||
final int currentDay = cal.get(Calendar.DAY_OF_MONTH); | ||
final int nextDay = currentDay + 1; | ||
|
||
final String currentDate = sdf.format(cal.getTime()); | ||
|
||
// Create a log message to be logged | ||
ExtLogRecord record = createLogRecord(Level.INFO, "Date: %s", currentDate); | ||
handler.publish(record); | ||
|
||
Assert.assertTrue("File '" + logFile + "' does not exist", Files.exists(logFile)); | ||
|
||
// Read the contents of the log file and ensure there's only one line | ||
List<String> lines = Files.readAllLines(logFile, StandardCharsets.UTF_8); | ||
Assert.assertEquals("More than 1 line found", 1, lines.size()); | ||
Assert.assertTrue("Expected the line to contain the date: " + currentDate, lines.get(0).contains(currentDate)); | ||
|
||
// Create a new record, increment the day by one. The file should fail rotating, but the contents of the | ||
// log file should contain the new data | ||
cal.add(Calendar.DAY_OF_MONTH, nextDay); | ||
final String nextDate = sdf.format(cal.getTime()); | ||
record = createLogRecord(Level.INFO, "Date: %s", nextDate); | ||
record.setMillis(cal.getTimeInMillis()); | ||
handler.publish(record); | ||
|
||
// Read the contents of the log file and ensure there's only one line | ||
lines = Files.readAllLines(logFile, StandardCharsets.UTF_8); | ||
Assert.assertEquals("More than 1 line found", 1, lines.size()); | ||
Assert.assertTrue("Expected the line to contain the date: " + nextDate, lines.get(0).contains(nextDate)); | ||
|
||
// The file should not have been rotated | ||
Assert.assertTrue("The rotated file '" + rotatedFile.toString() + "' exists and should not", Files.notExists(rotatedFile)); | ||
} | ||
|
||
|
||
private void testRotate(final Calendar cal, final Path rotatedFile) throws Exception { | ||
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | ||
|
@@ -105,7 +153,7 @@ private void testRotate(final Calendar cal, final Path rotatedFile) throws Excep | |
|
||
Assert.assertTrue("File '" + logFile + "' does not exist", Files.exists(logFile)); | ||
|
||
// Read the contents of the log file and ensure there's only one line and that like | ||
// Read the contents of the log file and ensure there's only one line | ||
List<String> lines = Files.readAllLines(logFile, StandardCharsets.UTF_8); | ||
Assert.assertEquals("More than 1 line found", 1, lines.size()); | ||
Assert.assertTrue("Expected the line to contain the date: " + currentDate, lines.get(0).contains(currentDate)); | ||
|
@@ -117,9 +165,8 @@ record = createLogRecord(Level.INFO, "Date: %s", nextDate); | |
record.setMillis(cal.getTimeInMillis()); | ||
handler.publish(record); | ||
|
||
// Read the contents of the log file and ensure there's only one line and that like | ||
// Read the contents of the log file and ensure there's only one line | ||
lines = Files.readAllLines(logFile, StandardCharsets.UTF_8); | ||
System.out.println(lines); | ||
Assert.assertEquals("More than 1 line found", 1, lines.size()); | ||
Assert.assertTrue("Expected the line to contain the date: " + nextDate, lines.get(0).contains(nextDate)); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
package org.jboss.logmanager.handlers; | ||
|
||
import java.io.File; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.text.SimpleDateFormat; | ||
|
@@ -31,14 +32,18 @@ | |
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.jboss.byteman.contrib.bmunit.BMRule; | ||
import org.jboss.byteman.contrib.bmunit.BMUnitRunner; | ||
import org.jboss.logmanager.ExtLogRecord; | ||
import org.junit.Assert; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">James R. Perkins</a> | ||
*/ | ||
@RunWith(BMUnitRunner.class) | ||
public class PeriodicSizeRotatingFileHandlerTests extends AbstractHandlerTest { | ||
private final static String FILENAME = "rotating-file-handler.log"; | ||
|
||
|
@@ -198,6 +203,38 @@ public void testArchiveRotateSizeOnlyZip() throws Exception { | |
testArchiveRotate(null,".zip"); | ||
} | ||
|
||
@Test | ||
@BMRule(name = "Test failed rotated", | ||
targetClass = "java.nio.file.Files", | ||
targetMethod = "move", | ||
targetLocation = "AT ENTRY", | ||
condition = "$2.getFileName().toString().equals(\"rotating-file-handler.log.2\")", | ||
action = "throw new IOException(\"Fail on purpose\")") | ||
public void testFailedRotate() throws Exception { | ||
final PeriodicSizeRotatingFileHandler handler = new PeriodicSizeRotatingFileHandler(); | ||
configureHandlerDefaults(handler); | ||
handler.setRotateSize(1024L); | ||
handler.setMaxBackupIndex(5); | ||
handler.setFile(logFile); | ||
|
||
// Allow a few rotates | ||
for (int i = 0; i < 100; i++) { | ||
handler.publish(createLogRecord("Test message: %d", i)); | ||
} | ||
|
||
handler.close(); | ||
|
||
// The log file should exist, as should one rotated file since we fail the rotation on the second rotate | ||
Assert.assertTrue(String.format("Expected log file %s to exist", logFile), logFile.exists()); | ||
final Path rotatedFile = BASE_LOG_DIR.toPath().resolve(FILENAME + ".1"); | ||
Assert.assertTrue(String.format("Expected rotated file %s to exist", rotatedFile), Files.exists(rotatedFile)); | ||
|
||
// The last line of the log file should end with "99" as it should be the last record | ||
final List<String> lines = Files.readAllLines(logFile.toPath(), StandardCharsets.UTF_8); | ||
final String lastLine = lines.get(lines.size() - 1); | ||
Assert.assertTrue("Expected the last line to end with 99: " + lastLine, lastLine.endsWith("99")); | ||
} | ||
|
||
private void testArchiveRotate(final String dateSuffix, final String archiveSuffix) throws Exception { | ||
final String currentDate = dateSuffix == null ? "" : LocalDate.now().format(DateTimeFormatter.ofPattern(dateSuffix)); | ||
PeriodicSizeRotatingFileHandler handler = new PeriodicSizeRotatingFileHandler(); | ||
|
Oops, something went wrong.