org.apache.logging.log4j.status.StatusData Java Examples

The following examples show how to use org.apache.logging.log4j.status.StatusData. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractLoggerTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testMessageThrows() {
    final ThrowableExpectingLogger logger = new ThrowableExpectingLogger(false);
    logger.error(new TestMessage(new TestMessage.FormattedMessageSupplier() {
        @Override
        public String getFormattedMessage() {
            throw new IllegalStateException("Oops!");
        }
    }, "Message Format"));
    List<StatusData> statusDatalist = StatusLogger.getLogger().getStatusData();
    StatusData mostRecent = statusDatalist.get(statusDatalist.size() - 1);
    assertEquals(Level.WARN, mostRecent.getLevel());
    assertThat(mostRecent.getFormattedStatus(), containsString(
            "org.apache.logging.log4j.spi.AbstractLogger caught " +
                    "java.lang.IllegalStateException logging TestMessage: Message Format"));
}
 
Example #2
Source File: AbstractLoggerTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testMessageThrowsAndNullFormat() {
    final ThrowableExpectingLogger logger = new ThrowableExpectingLogger(false);
    logger.error(new TestMessage(new TestMessage.FormattedMessageSupplier() {
        @Override
        public String getFormattedMessage() {
            throw new IllegalStateException("Oops!");
        }
    }, null /* format */));
    List<StatusData> statusDatalist = StatusLogger.getLogger().getStatusData();
    StatusData mostRecent = statusDatalist.get(statusDatalist.size() - 1);
    assertEquals(Level.WARN, mostRecent.getLevel());
    assertThat(mostRecent.getFormattedStatus(), containsString(
            "org.apache.logging.log4j.spi.AbstractLogger caught " +
                    "java.lang.IllegalStateException logging TestMessage: "));
}
 
Example #3
Source File: AbstractActionTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testErrorsAreLoggedToStatusLogger() {
    StatusLogger statusLogger = StatusLogger.getLogger();
    statusLogger.clear();
    new AbstractAction() {
        @Override
        public boolean execute() {
            throw new AssertionError();
        }
    }.run();
    List<StatusData> statusDataList = statusLogger.getStatusData();
    assertEquals(1, statusDataList.size());
    StatusData statusData = statusDataList.get(0);
    assertEquals(Level.WARN, statusData.getLevel());
    String formattedMessage = statusData.getFormattedStatus();
    assertTrue(formattedMessage.contains("Exception reported by action"));
}
 
Example #4
Source File: AbstractActionTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@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();
    assertEquals(1, statusDataList.size());
    StatusData statusData = statusDataList.get(0);
    assertEquals(Level.WARN, statusData.getLevel());
    String formattedMessage = statusData.getFormattedStatus();
    assertTrue(formattedMessage.contains("Exception reported by action"));
}
 
Example #5
Source File: AbstractActionTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testExceptionsAreLoggedToStatusLogger() {
    StatusLogger statusLogger = StatusLogger.getLogger();
    statusLogger.clear();
    new TestAction().run();
    List<StatusData> statusDataList = statusLogger.getStatusData();
    assertEquals(1, statusDataList.size());
    StatusData statusData = statusDataList.get(0);
    assertEquals(Level.WARN, statusData.getLevel());
    String formattedMessage = statusData.getFormattedStatus();
    assertTrue(formattedMessage, formattedMessage.contains("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:"));
}
 
Example #6
Source File: ValidatingPluginWithFailoverTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testDoesNotLog_NoParameterThatMatchesElement_message() {
    final StoringStatusListener listener = new StoringStatusListener();
    // @formatter:off
    final PluginBuilder builder = new PluginBuilder(plugin).
            setConfiguration(new NullConfiguration()).
            setConfigurationNode(node);
    // @formatter:on
    StatusLogger.getLogger().registerListener(listener);

    final FailoverAppender failoverAppender = (FailoverAppender) builder.build();

    assertThat(listener.logs, emptyCollectionOf(StatusData.class));
    assertNotNull(failoverAppender);
    assertEquals("Failover", failoverAppender.getName());
}
 
Example #7
Source File: LoggersPluginTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmptyAttribute() throws Exception {
    final Logger logger = LogManager.getLogger();
    logger.info("Test");
    final StatusData data = StatusLogger.getLogger().getStatusData().get(0);
    //System.out.println(data.getFormattedStatus());

    assertEquals(Level.ERROR, data.getLevel());
    assertTrue(data.getMessage().getFormattedMessage().contains("multiple root loggers"));
}
 
Example #8
Source File: QueueFullAsyncLoggerConfigLoggingFromToStringTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
static void asyncLoggerConfigRecursiveTest(final Logger logger,
                                           final Unlocker unlocker,
                                           final BlockingAppender blockingAppender,
                                           final QueueFullAbstractTest factory) {
    for (int i = 0; i < 1; i++) {
        TRACE("Test logging message " + i  + ". Remaining capacity=" + asyncRemainingCapacity(logger));
        TRACE("Test decrementing unlocker countdown latch. Count=" + unlocker.countDownLatch.getCount());
        unlocker.countDownLatch.countDown();
        final DomainObject obj = factory.new DomainObject(129);
        logger.info("logging naughty object #{} {}", i, obj);
    }
    TRACE("Before stop() blockingAppender.logEvents.count=" + blockingAppender.logEvents.size());
    //CoreLoggerContexts.stopLoggerContext(false); // stop async thread
    while (blockingAppender.logEvents.size() < 130) { Thread.yield(); }
    TRACE("After  stop() blockingAppender.logEvents.count=" + blockingAppender.logEvents.size());

    final Stack<String> actual = transform(blockingAppender.logEvents);
    assertEquals("Logging in toString() #0", actual.pop());
    List<StatusData> statusDataList = StatusLogger.getLogger().getStatusData();
    assertEquals("Jumped the queue: queue full",
            "Logging in toString() #128", actual.pop());
    StatusData mostRecentStatusData = statusDataList.get(statusDataList.size() - 1);
    assertEquals("Expected warn level status message", Level.WARN, mostRecentStatusData.getLevel());
    assertThat(mostRecentStatusData.getFormattedStatus(), containsString(
            "Log4j2 logged an event out of order to prevent deadlock caused by domain " +
                    "objects logging from their toString method when the async queue is full"));

    for (int i = 1; i < 128; i++) {
        assertEquals("First batch", "Logging in toString() #" + i, actual.pop());
    }
    assertEquals("logging naughty object #0 Who's bad?!", actual.pop());
    assertTrue(actual.isEmpty());
}
 
Example #9
Source File: OutputStreamManagerTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void narrow() throws Exception {
    final Logger logger = LogManager.getLogger(OutputStreamManagerTest.class);
    logger.info("test");
    final List<StatusData> statusData = StatusLogger.getLogger().getStatusData();
    StatusData data = statusData.get(0);
    if (data.getMessage().getFormattedMessage().contains("WindowsAnsiOutputStream")) {
        data = statusData.get(1);
    }
    assertEquals(Level.ERROR, data.getLevel());
    assertEquals("Could not create plugin of type class org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender for element RollingRandomAccessFile",
            data.getMessage().getFormattedMessage());
    assertEquals("org.apache.logging.log4j.core.config.ConfigurationException: Configuration has multiple incompatible Appenders pointing to the same resource 'target/multiIncompatibleAppender.log'",
            data.getThrowable().toString());
}
 
Example #10
Source File: RollingAppenderDirectWrite1906Test.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private String logFileNameError(String expected, String actual) {
    final List<StatusData> statusData = StatusLogger.getLogger().getStatusData();
    final StringBuilder sb = new StringBuilder();
    for (StatusData statusItem : statusData) {
        sb.append(statusItem.getFormattedStatus());
        sb.append("\n");
    }
    sb.append("Incorrect file name. Expected: ").append(expected).append(" Actual: ").append(actual);
    return sb.toString();
}
 
Example #11
Source File: StatusLoggerAdmin.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public void log(final StatusData data) {
    final Notification notifMsg = new Notification(NOTIF_TYPE_MESSAGE, getObjectName(), nextSeqNo(), nowMillis(),
            data.getFormattedStatus());
    sendNotification(notifMsg);

    final Notification notifData = new Notification(NOTIF_TYPE_DATA, getObjectName(), nextSeqNo(), nowMillis());
    notifData.setUserData(data);
    sendNotification(notifData);
}
 
Example #12
Source File: StatusLoggerAdmin.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public String[] getStatusDataHistory() {
    final List<StatusData> data = getStatusData();
    final String[] result = new String[data.size()];
    for (int i = 0; i < result.length; i++) {
        result[i] = data.get(i).getFormattedStatus();
    }
    return result;
}
 
Example #13
Source File: HttpAppenderTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testAppendErrorIgnore() throws Exception {
    wireMockRule.stubFor(post(urlEqualTo("/test/log4j/"))
        .willReturn(FAILURE_RESPONSE));

    StatusLogger.getLogger().registerListener(new StatusListener() {
        @Override
        public void log(final StatusData data) {
            error = data;
        }

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

        @Override
        public void close() throws IOException { }
    });

    error = null;

    final Appender appender = HttpAppender.newBuilder()
        .setName("Http")
        .setLayout(JsonLayout.createDefaultLayout())
        .setConfiguration(ctx.getConfiguration())
        .setUrl(new URL("http://localhost:" + wireMockRule.port() + "/test/log4j/"))
        .build();
    appender.append(createLogEvent());

    wireMockRule.verify(postRequestedFor(urlEqualTo("/test/log4j/"))
        .withHeader("Host", containing("localhost"))
        .withHeader("Content-Type", containing("application/json"))
        .withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE + "\"")));

    assertNotNull(error);
    assertEquals(Level.ERROR, error.getLevel());
    assertEquals("Unable to send HTTP in appender [Http]", error.getMessage().toString());
}
 
Example #14
Source File: LogConfigurator.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public void log(StatusData data) {
    ERROR.set(true);
    super.log(data);
}
 
Example #15
Source File: ValidatingPluginWithFailoverTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public void log(StatusData data) {
    logs.add(data);
}
 
Example #16
Source File: StatusLoggerAdmin.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public List<StatusData> getStatusData() {
    return StatusLogger.getLogger().getStatusData();
}
 
Example #17
Source File: ESTestCase.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public void log(StatusData data) {
    synchronized (statusData) {
        statusData.add(data);
    }
}
 
Example #18
Source File: StatusLoggerAdminMBean.java    From logging-log4j2 with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a list with the most recent {@code StatusData} objects in the
 * status history. The list has up to 200 entries by default but the length
 * can be configured with system property {@code "log4j2.status.entries"}.
 * <p>
 * Note that the returned objects may contain {@code Throwable}s from
 * external libraries.
 * </p>
 * <p>
 * JMX clients calling this method must be prepared to deal with the errors
 * that occur if they do not have the class definition for such
 * {@code Throwable}s in their classpath.
 * </p>
 *
 * @return the most recent messages logged by the {@code StatusLogger}.
 */
List<StatusData> getStatusData();
 
Example #19
Source File: RollingAppenderDirectWrite1906Test.java    From logging-log4j2 with Apache License 2.0 2 votes vote down vote up
@Override
public void log(StatusData data) {

}