Search in sources :

Example 1 with FailOnceAppender

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);
    }
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) AsyncAppender(org.apache.logging.log4j.core.appender.AsyncAppender) LogEvent(org.apache.logging.log4j.core.LogEvent) Logger(org.apache.logging.log4j.core.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) FailOnceAppender(org.apache.logging.log4j.core.test.appender.FailOnceAppender) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

LogEvent (org.apache.logging.log4j.core.LogEvent)1 Logger (org.apache.logging.log4j.core.Logger)1 LoggerContext (org.apache.logging.log4j.core.LoggerContext)1 AsyncAppender (org.apache.logging.log4j.core.appender.AsyncAppender)1 Configuration (org.apache.logging.log4j.core.config.Configuration)1 FailOnceAppender (org.apache.logging.log4j.core.test.appender.FailOnceAppender)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 ValueSource (org.junit.jupiter.params.provider.ValueSource)1