use of org.apache.logging.log4j.core.test.appender.FailOnceAppender in project logging-log4j2 by apache.
the class AsyncAppenderExceptionHandlingTest method AsyncAppender_should_not_stop_on_appender_failures.
@ParameterizedTest
@ValueSource(strings = { FailOnceAppender.ThrowableClassName.RUNTIME_EXCEPTION, FailOnceAppender.ThrowableClassName.LOGGING_EXCEPTION, FailOnceAppender.ThrowableClassName.EXCEPTION, FailOnceAppender.ThrowableClassName.ERROR, FailOnceAppender.ThrowableClassName.THROWABLE, FailOnceAppender.ThrowableClassName.THREAD_DEATH })
void AsyncAppender_should_not_stop_on_appender_failures(String throwableClassName) {
// Create the logger.
final String throwableClassNamePropertyName = "throwableClassName";
System.setProperty(throwableClassNamePropertyName, throwableClassName);
try (final LoggerContext loggerContext = Configurator.initialize("Test", "AsyncAppenderExceptionHandlingTest.xml")) {
final Logger logger = loggerContext.getRootLogger();
// Log the 1st message, which should fail due to the FailOnceAppender.
logger.info("message #1");
// Log the 2nd message, which should succeed.
final String lastLogMessage = "message #2";
logger.info(lastLogMessage);
// Stop the AsyncAppender to drain the queued events.
Configuration configuration = loggerContext.getConfiguration();
AsyncAppender asyncAppender = configuration.getAppender("Async");
Assertions.assertNotNull(asyncAppender, "couldn't obtain the FailOnceAppender");
asyncAppender.stop();
// Verify the logged message.
final FailOnceAppender failOnceAppender = configuration.getAppender("FailOnce");
Assertions.assertNotNull(failOnceAppender, "couldn't obtain the FailOnceAppender");
Assertions.assertTrue(failOnceAppender.isFailed(), "FailOnceAppender hasn't failed yet");
final List<String> accumulatedMessages = failOnceAppender.drainEvents().stream().map(LogEvent::getMessage).map(Message::getFormattedMessage).collect(Collectors.toList());
Assertions.assertEquals(Collections.singletonList(lastLogMessage), accumulatedMessages);
} finally {
System.setProperty(throwableClassNamePropertyName, Strings.EMPTY);
}
}
Aggregations