Search in sources :

Example 36 with ListAppender

use of org.apache.logging.log4j.test.appender.ListAppender in project logging-log4j2 by apache.

the class CallerInformationTest method testMethodLogger.

@Test
public void testMethodLogger() throws Exception {
    final ListAppender app = ctx.getListAppender("Method").clear();
    final Log logger = LogFactory.getLog("MethodLogger");
    logger.info("More messages.");
    logger.warn("CATASTROPHE INCOMING!");
    logger.error("ZOMBIES!!!");
    logger.warn("brains~~~");
    logger.info("Itchy. Tasty.");
    final List<String> messages = app.getMessages();
    assertEquals("Incorrect number of messages.", 5, messages.size());
    for (final String message : messages) {
        assertEquals("Incorrect caller method name.", "testMethodLogger", message);
    }
}
Also used : Log(org.apache.commons.logging.Log) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Test(org.junit.Test)

Example 37 with ListAppender

use of org.apache.logging.log4j.test.appender.ListAppender in project logging-log4j2 by apache.

the class LoggerTest method verify.

private void verify(final String name, final String expected) {
    final ListAppender listApp = context.getListAppender(name);
    final List<String> events = listApp.getMessages();
    assertThat(events, hasSize(1));
    final String actual = events.get(0);
    assertThat(actual, equalTo(expected));
    listApp.clear();
}
Also used : ListAppender(org.apache.logging.log4j.test.appender.ListAppender)

Example 38 with ListAppender

use of org.apache.logging.log4j.test.appender.ListAppender in project logging-log4j2 by apache.

the class MapFilterTest method testConfig.

@Test
public void testConfig() {
    final Configuration config = context.getConfiguration();
    final Filter filter = config.getFilter();
    assertNotNull("No MapFilter", filter);
    assertTrue("Not a MapFilter", filter instanceof MapFilter);
    final MapFilter mapFilter = (MapFilter) filter;
    assertFalse("Should not be And filter", mapFilter.isAnd());
    final Map<String, List<String>> map = mapFilter.getMap();
    assertNotNull("No Map", map);
    assertFalse("No elements in Map", map.isEmpty());
    assertEquals("Incorrect number of elements in Map", 1, map.size());
    assertTrue("Map does not contain key eventId", map.containsKey("eventId"));
    assertEquals("List does not contain 2 elements", 2, map.get("eventId").size());
    final Logger logger = LogManager.getLogger(MapFilterTest.class);
    final Map<String, String> eventMap = new HashMap<>();
    eventMap.put("eventId", "Login");
    logger.debug(new MapMessage(eventMap));
    final ListAppender app = context.getListAppender("LIST");
    final List<String> msgs = app.getMessages();
    assertNotNull("No messages", msgs);
    assertFalse("No messages", msgs.isEmpty());
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) Filter(org.apache.logging.log4j.core.Filter) HashMap(java.util.HashMap) MapMessage(org.apache.logging.log4j.message.MapMessage) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Test(org.junit.Test)

Example 39 with ListAppender

use of org.apache.logging.log4j.test.appender.ListAppender in project logging-log4j2 by apache.

the class DefaultRouteScriptAppenderTest method getListAppender.

private ListAppender getListAppender() {
    final String key = "Service2";
    final RoutingAppender routingAppender = getRoutingAppender();
    Assert.assertTrue(routingAppender.isStarted());
    final Map<String, AppenderControl> appenders = routingAppender.getAppenders();
    final AppenderControl appenderControl = appenders.get(key);
    assertNotNull("No appender control generated for '" + key + "'; appenders = " + appenders, appenderControl);
    final ListAppender listAppender = (ListAppender) appenderControl.getAppender();
    return listAppender;
}
Also used : AppenderControl(org.apache.logging.log4j.core.config.AppenderControl) ListAppender(org.apache.logging.log4j.test.appender.ListAppender)

Example 40 with ListAppender

use of org.apache.logging.log4j.test.appender.ListAppender in project logging-log4j2 by apache.

the class DefaultRouteScriptAppenderTest method logAndCheck.

private void logAndCheck() {
    final Marker marker = MarkerManager.getMarker("HEXDUMP");
    final Logger logger = loggerContextRule.getLogger(DefaultRouteScriptAppenderTest.class);
    logger.error("Hello");
    final ListAppender listAppender = getListAppender();
    final List<LogEvent> list = listAppender.getEvents();
    assertNotNull("No events generated", list);
    assertTrue("Incorrect number of events. Expected 1, got " + list.size(), list.size() == 1);
    logger.error("World");
    assertTrue("Incorrect number of events. Expected 2, got " + list.size(), list.size() == 2);
    logger.error(marker, "DEADBEEF");
    assertTrue("Incorrect number of events. Expected 3, got " + list.size(), list.size() == 3);
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Marker(org.apache.logging.log4j.Marker) Logger(org.apache.logging.log4j.core.Logger)

Aggregations

ListAppender (org.apache.logging.log4j.test.appender.ListAppender)64 Test (org.junit.Test)46 Appender (org.apache.logging.log4j.core.Appender)17 Logger (org.apache.logging.log4j.Logger)8 LogEvent (org.apache.logging.log4j.core.LogEvent)6 Configuration (org.apache.logging.log4j.core.config.Configuration)5 AbstractLogger (org.apache.logging.log4j.spi.AbstractLogger)5 Marker (org.apache.logging.log4j.Marker)4 Logger (org.apache.logging.log4j.core.Logger)4 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)4 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Logger (java.util.logging.Logger)2 Log (org.apache.commons.logging.Log)2 AppenderControl (org.apache.logging.log4j.core.config.AppenderControl)2 Logger (org.slf4j.Logger)2 InputStream (java.io.InputStream)1 ObjectInputStream (java.io.ObjectInputStream)1