Search in sources :

Example 1 with StatusLogger

use of org.apache.logging.log4j.status.StatusLogger in project logging-log4j2 by apache.

the class AbstractActionTest method testExceptionsAreLoggedToStatusLogger.

// Test for LOG4J2-2658
@Test
public void testExceptionsAreLoggedToStatusLogger() {
    StatusLogger statusLogger = StatusLogger.getLogger();
    statusLogger.clear();
    new TestAction().run();
    List<StatusData> statusDataList = statusLogger.getStatusData();
    assertThat(statusDataList, hasSize(1));
    StatusData statusData = statusDataList.get(0);
    assertEquals(Level.WARN, statusData.getLevel());
    String formattedMessage = statusData.getFormattedStatus();
    assertThat(formattedMessage, containsString("Exception reported by action 'class org.apache." + "logging.log4j.core.appender.rolling.action.AbstractActionTest$TestAction' java.io.IOException: " + "failed" + System.lineSeparator() + "\tat org.apache.logging.log4j.core.appender.rolling.action.AbstractActionTest" + "$TestAction.execute(AbstractActionTest.java:"));
}
Also used : StatusLogger(org.apache.logging.log4j.status.StatusLogger) Matchers.containsString(org.hamcrest.Matchers.containsString) StatusData(org.apache.logging.log4j.status.StatusData) Test(org.junit.jupiter.api.Test)

Example 2 with StatusLogger

use of org.apache.logging.log4j.status.StatusLogger in project logging-log4j2 by apache.

the class AbstractActionTest method testRuntimeExceptionsAreLoggedToStatusLogger.

@Test
public void testRuntimeExceptionsAreLoggedToStatusLogger() {
    StatusLogger statusLogger = StatusLogger.getLogger();
    statusLogger.clear();
    new AbstractAction() {

        @Override
        public boolean execute() {
            throw new IllegalStateException();
        }
    }.run();
    List<StatusData> statusDataList = statusLogger.getStatusData();
    assertThat(statusDataList, hasSize(1));
    StatusData statusData = statusDataList.get(0);
    assertEquals(Level.WARN, statusData.getLevel());
    String formattedMessage = statusData.getFormattedStatus();
    assertThat(formattedMessage, containsString("Exception reported by action"));
}
Also used : StatusLogger(org.apache.logging.log4j.status.StatusLogger) Matchers.containsString(org.hamcrest.Matchers.containsString) StatusData(org.apache.logging.log4j.status.StatusData) Test(org.junit.jupiter.api.Test)

Example 3 with StatusLogger

use of org.apache.logging.log4j.status.StatusLogger in project logging-log4j2 by apache.

the class StatusLoggerAdmin method removeListeners.

/**
 * Add listener to StatusLogger for this context, or replace it if it already exists.
 *
 * @param ctxName
 */
private void removeListeners(final String ctxName) {
    final StatusLogger logger = StatusLogger.getLogger();
    final Iterable<StatusListener> listeners = logger.getListeners();
    // Remove any StatusLoggerAdmin listeners already registered for this context
    for (final StatusListener statusListener : listeners) {
        if (statusListener instanceof StatusLoggerAdmin) {
            final StatusLoggerAdmin adminListener = (StatusLoggerAdmin) statusListener;
            if (ctxName != null && ctxName.equals(adminListener.contextName)) {
                logger.removeListener(adminListener);
            }
        }
    }
}
Also used : StatusLogger(org.apache.logging.log4j.status.StatusLogger) StatusListener(org.apache.logging.log4j.status.StatusListener)

Example 4 with StatusLogger

use of org.apache.logging.log4j.status.StatusLogger in project logging-log4j2 by apache.

the class LoggerContextAnchorTest method getAnchorFqcn.

private static String getAnchorFqcn(Runnable runnable) {
    List<String> results = new CopyOnWriteArrayList<>();
    StatusListener listener = new StatusListener() {

        @Override
        public void log(StatusData data) {
            String formattedMessage = data.getMessage().getFormattedMessage();
            if (formattedMessage.startsWith(PREFIX)) {
                results.add(formattedMessage.substring(PREFIX.length()));
            }
        }

        @Override
        public Level getStatusLevel() {
            return Level.TRACE;
        }

        @Override
        public void close() {
        // nop
        }
    };
    StatusLogger statusLogger = StatusLogger.getLogger();
    statusLogger.registerListener(listener);
    try {
        runnable.run();
        if (results.isEmpty()) {
            throw new AssertionError("Failed to locate an anchor lookup status message");
        }
        if (results.size() > 1) {
            throw new AssertionError("Found multiple anchor lines: " + results);
        }
        return results.get(0);
    } finally {
        statusLogger.removeListener(listener);
    }
}
Also used : StatusLogger(org.apache.logging.log4j.status.StatusLogger) StatusListener(org.apache.logging.log4j.status.StatusListener) StatusData(org.apache.logging.log4j.status.StatusData) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 5 with StatusLogger

use of org.apache.logging.log4j.status.StatusLogger in project logging-log4j2 by apache.

the class StatusLoggerLevelExtension method beforeEach.

@Override
public void beforeEach(ExtensionContext context) throws Exception {
    final StatusLoggerLevel annotation = context.getRequiredTestClass().getAnnotation(StatusLoggerLevel.class);
    if (annotation == null) {
        return;
    }
    final StatusLogger logger = StatusLogger.getLogger();
    getStore(context).put(KEY, logger.getLevel());
    logger.setLevel(Level.valueOf(annotation.value()));
}
Also used : StatusLogger(org.apache.logging.log4j.status.StatusLogger)

Aggregations

StatusLogger (org.apache.logging.log4j.status.StatusLogger)6 StatusData (org.apache.logging.log4j.status.StatusData)4 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Test (org.junit.jupiter.api.Test)3 StatusListener (org.apache.logging.log4j.status.StatusListener)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1