Search in sources :

Example 1 with ListAppender

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

the class PropertyTest method testEmptyAttribute.

@Test
public void testEmptyAttribute() throws Exception {
    final org.apache.logging.log4j.Logger logger = LogManager.getLogger();
    logger.info("msg");
    final ListAppender app = (ListAppender) context.getRequiredAppender("List");
    assertNotNull("No ListAppender", app);
    final List<String> messages = app.getMessages();
    assertNotNull("No Messages", messages);
    assertEquals("message count" + messages, 1, messages.size());
    //<Property name="emptyElementKey" />
    //<Property name="emptyAttributeKey" value="" />
    //<Property name="emptyAttributeKey2" value=""></Property>
    //<Property name="elementKey">elementValue</Property>
    //<Property name="attributeKey" value="attributeValue" />
    //<Property name="attributeWithEmptyElementKey" value="attributeValue2"></Property>
    //<Property name="bothElementAndAttributeKey" value="attributeValue3">elementValue3</Property>
    final String expect = // ${sys:elementKey}
    "1=elementValue" + // ${sys:emptyElementKey}
    ",2=" + // ${sys:emptyAttributeKey}
    ",a=" + // ${sys:emptyAttributeKey2}
    ",b=" + // ${sys:attributeKey}
    ",3=attributeValue" + // ${sys:attributeWithEmptyElementKey}
    ",4=attributeValue2" + // ${sys:bothElementAndAttributeKey}
    ",5=elementValue3,m=msg";
    assertEquals(expect, messages.get(0));
    app.clear();
}
Also used : ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Test(org.junit.Test)

Example 2 with ListAppender

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

the class IoBuilderCallerInfoTesting method assertMessages.

public void assertMessages(final String msg, final int size, final String methodName) {
    final ListAppender appender = ctx.getListAppender("ClassAndMethod");
    assertEquals(msg + ".size", size, appender.getMessages().size());
    for (final String message : appender.getMessages()) {
        assertEquals(msg + " has incorrect caller info", this.getClass().getName() + '.' + methodName, message);
    }
}
Also used : ListAppender(org.apache.logging.log4j.test.appender.ListAppender)

Example 3 with ListAppender

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

the class IoBuilderTest method testNoArgBuilderCallerClassInfo.

@Test
public void testNoArgBuilderCallerClassInfo() throws Exception {
    try (final PrintStream ps = IoBuilder.forLogger().buildPrintStream()) {
        ps.println("discarded");
        final ListAppender app = context.getListAppender("IoBuilderTest");
        final List<String> messages = app.getMessages();
        assertThat(messages, not(empty()));
        assertThat(messages, hasSize(1));
        final String message = messages.get(0);
        assertThat(message, startsWith(getClass().getName() + ".testNoArgBuilderCallerClassInfo"));
        app.clear();
    }
}
Also used : PrintStream(java.io.PrintStream) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Test(org.junit.Test)

Example 4 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.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 : ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Test(org.junit.Test)

Example 5 with ListAppender

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

the class CategoryTest method testClassName.

@Test
public void testClassName() {
    final Category category = Category.getInstance("TestCategory");
    final Layout<String> layout = PatternLayout.newBuilder().withPattern("%d %p %C{1.} [%t] %m%n").build();
    final ListAppender appender = new ListAppender("List2", null, layout, false, false);
    appender.start();
    category.setAdditivity(false);
    category.getLogger().addAppender(appender);
    category.error("Test Message");
    final List<String> msgs = appender.getMessages();
    assertTrue("Incorrect number of messages. Expected 1 got " + msgs.size(), msgs.size() == 1);
    final String msg = msgs.get(0);
    appender.clear();
    final String threadName = Thread.currentThread().getName();
    final String expected = "ERROR o.a.l.CategoryTest [" + threadName + "] Test Message" + Strings.LINE_SEPARATOR;
    assertTrue("Incorrect message " + Strings.dquote(msg) + " expected " + Strings.dquote(expected), msg.endsWith(expected));
}
Also used : 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