Search in sources :

Example 61 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 = context.getListAppender("Method").clear();
    final Logger logger = context.getLogger("MethodLogger");
    logger.info("More messages.");
    logger.warn("CATASTROPHE INCOMING!");
    logger.error("ZOMBIES!!!");
    logger.fatal("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 : ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Logger(org.apache.logging.log4j.Logger) Test(org.junit.Test)

Example 62 with ListAppender

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

the class SequenceNumberPatternConverterTest method testSequenceIncreases.

@Test
public void testSequenceIncreases() throws Exception {
    final Logger logger = ctx.getLogger();
    logger.info("Message 1");
    logger.info("Message 2");
    logger.info("Message 3");
    logger.info("Message 4");
    logger.info("Message 5");
    final ListAppender app = ctx.getListAppender("List");
    final List<String> messages = app.getMessages();
    assertThat(messages, contains("1", "2", "3", "4", "5"));
}
Also used : ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Logger(org.apache.logging.log4j.Logger) Test(org.junit.Test)

Example 63 with ListAppender

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

the class PatternSelectorTest method testScriptPatternSelector.

@Test
public void testScriptPatternSelector() throws Exception {
    final org.apache.logging.log4j.Logger logger = LogManager.getLogger("TestScriptPatternSelector");
    final org.apache.logging.log4j.Logger logger2 = LogManager.getLogger("NoLocation");
    logger.traceEntry();
    logger.info("Hello World");
    logger2.info("No location information");
    logger.traceExit();
    final ListAppender app = (ListAppender) context.getRequiredAppender("List2");
    assertNotNull("No ListAppender", app);
    final List<String> messages = app.getMessages();
    assertNotNull("No Messages", messages);
    assertTrue("Incorrect number of messages. Expected 4, Actual " + messages.size() + ": " + messages, messages.size() == 4);
    String expect = "[TRACE] TestScriptPatternSelector ====== " + "o.a.l.l.c.PatternSelectorTest.testScriptPatternSelector:62 Enter ======" + Strings.LINE_SEPARATOR;
    assertEquals(expect, messages.get(0));
    expect = "[INFO ] TestScriptPatternSelector o.a.l.l.c.PatternSelectorTest.testScriptPatternSelector.63 " + "Hello World" + Strings.LINE_SEPARATOR;
    assertEquals(expect, messages.get(1));
    assertEquals("[INFO ] NoLocation No location information" + Strings.LINE_SEPARATOR, messages.get(2));
    app.clear();
}
Also used : ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Test(org.junit.Test)

Example 64 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)

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