Search in sources :

Example 31 with ListAppender

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

the class XmlLoggerPropsTest method testWithProps.

@Test
public void testWithProps() {
    final ListAppender listAppender = context.getListAppender("List");
    assertNotNull("No List Appender", listAppender);
    try {
        assertThat(context.getConfiguration(), is(instanceOf(XmlConfiguration.class)));
        Logger logger = LogManager.getLogger(XmlLoggerPropsTest.class);
        logger.debug("Test with props");
        logger = LogManager.getLogger("tiny.bubbles");
        logger.debug("Test on root");
        final List<String> events = listAppender.getMessages();
        assertTrue("No events", events.size() > 0);
        assertTrue("Incorrect number of events", events.size() == 2);
        assertThat(events.get(0), allOf(containsString("user="), containsString("phrasex=****"), containsString("test=test"), containsString("test2=test2default"), containsString("test3=Unknown"), containsString("test4=test"), containsString("test5=test"), containsString("attribKey=attribValue"), containsString("duplicateKey=nodeValue")));
        assertThat(events.get(1), allOf(containsString("user="), containsString("phrasex=****"), containsString("test=test"), containsString("test2=test2default"), containsString("test3=Unknown"), containsString("test4=test"), containsString("test5=test"), containsString("attribKey=attribValue"), containsString("duplicateKey=nodeValue")));
    } finally {
        System.clearProperty("test");
    }
}
Also used : ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Matchers.containsString(org.hamcrest.Matchers.containsString) Logger(org.apache.logging.log4j.Logger) Test(org.junit.Test)

Example 32 with ListAppender

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

the class LoggingTest method verify.

private void verify(final String expected) {
    final ListAppender listApp = ListAppender.getListAppender("List");
    assertNotNull("Missing Appender", listApp);
    final List<String> events = listApp.getMessages();
    assertTrue("Incorrect number of messages. Expected 1 Actual " + events.size(), events.size() == 1);
    final String actual = events.get(0);
    assertEquals("Incorrect message. Expected " + expected + ". Actual " + actual, expected, actual);
    listApp.clear();
}
Also used : ListAppender(org.apache.logging.log4j.test.appender.ListAppender)

Example 33 with ListAppender

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

the class CallerInformationTest method testClassLogger.

@Test
public void testClassLogger() throws Exception {
    final ListAppender app = ctx.getListAppender("Class").clear();
    final Logger logger = Logger.getLogger("ClassLogger");
    logger.info("Ignored message contents.");
    logger.warning("Verifying the caller class is still correct.");
    logger.severe("Hopefully nobody breaks me!");
    final List<String> messages = app.getMessages();
    assertEquals("Incorrect number of messages.", 3, messages.size());
    for (final String message : messages) {
        assertEquals("Incorrect caller class name.", this.getClass().getName(), message);
    }
}
Also used : ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Logger(java.util.logging.Logger) Test(org.junit.Test)

Example 34 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 Logger logger = Logger.getLogger("MethodLogger");
    logger.info("More messages.");
    logger.warning("CATASTROPHE INCOMING!");
    logger.severe("ZOMBIES!!!");
    logger.warning("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(java.util.logging.Logger) Test(org.junit.Test)

Example 35 with ListAppender

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

the class CallerInformationTest method testClassLogger.

@Test
public void testClassLogger() throws Exception {
    final ListAppender app = ctx.getListAppender("Class").clear();
    final Log logger = LogFactory.getLog("ClassLogger");
    logger.info("Ignored message contents.");
    logger.warn("Verifying the caller class is still correct.");
    logger.error("Hopefully nobody breaks me!");
    final List<String> messages = app.getMessages();
    assertEquals("Incorrect number of messages.", 3, messages.size());
    for (final String message : messages) {
        assertEquals("Incorrect caller class name.", this.getClass().getName(), message);
    }
}
Also used : Log(org.apache.commons.logging.Log) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) 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