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:"));
}
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"));
}
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);
}
}
}
}
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);
}
}
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()));
}
Aggregations