Search in sources :

Example 56 with ListAppender

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

the class SyslogLayoutTest method testLayout.

/**
     * Test case for MDC conversion pattern.
     */
@Test
public void testLayout() throws Exception {
    for (final Appender appender : root.getAppenders().values()) {
        root.removeAppender(appender);
    }
    // set up appender
    // @formatter:off
    final SyslogLayout layout = SyslogLayout.newBuilder().setFacility(Facility.LOCAL0).setIncludeNewLine(true).build();
    // @formatter:on
    //ConsoleAppender appender = new ConsoleAppender("Console", layout);
    final ListAppender appender = new ListAppender("List", null, layout, true, false);
    appender.start();
    // set appender on root and set level to debug
    root.addAppender(appender);
    root.setLevel(Level.DEBUG);
    // output starting message
    root.debug("starting mdc pattern test");
    root.debug("empty mdc");
    ThreadContext.put("key1", "value1");
    ThreadContext.put("key2", "value2");
    root.debug("filled mdc");
    ThreadContext.put("loginId", "JohnDoe");
    ThreadContext.put("ipAddress", "192.168.0.120");
    ThreadContext.put("locale", Locale.US.getDisplayName());
    final StructuredDataMessage msg = new StructuredDataMessage("Transfer@18060", "Transfer Complete", "Audit");
    msg.put("ToAccount", "123456");
    msg.put("FromAccount", "123457");
    msg.put("Amount", "200.00");
    root.info(MarkerManager.getMarker("EVENT"), msg);
    appender.stop();
    final List<String> list = appender.getMessages();
    assertTrue("Expected line 1 to end with: " + line1 + " Actual " + list.get(0), list.get(0).endsWith(line1));
    assertTrue("Expected line 2 to end with: " + line2 + " Actual " + list.get(1), list.get(1).endsWith(line2));
    assertTrue("Expected line 3 to end with: " + line3 + " Actual " + list.get(2), list.get(2).endsWith(line3));
    assertTrue("Expected line 4 to end with: " + line4 + " Actual " + list.get(3), list.get(3).endsWith(line4));
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Test(org.junit.Test)

Example 57 with ListAppender

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

the class XmlLayoutTest method testLayout.

/**
     * Test case for MDC conversion pattern.
     */
@Test
public void testLayout() throws Exception {
    final Map<String, Appender> appenders = this.rootLogger.getAppenders();
    for (final Appender appender : appenders.values()) {
        this.rootLogger.removeAppender(appender);
    }
    // set up appender
    final XmlLayout layout = XmlLayout.createLayout(true, true, true, false, null, true);
    final ListAppender appender = new ListAppender("List", null, layout, true, false);
    appender.start();
    // set appender on root and set level to debug
    this.rootLogger.addAppender(appender);
    this.rootLogger.setLevel(Level.DEBUG);
    // output starting message
    this.rootLogger.debug("starting mdc pattern test");
    this.rootLogger.debug("empty mdc");
    ThreadContext.put("key1", "value1");
    ThreadContext.put("key2", "value2");
    this.rootLogger.debug("filled mdc");
    ThreadContext.remove("key1");
    ThreadContext.remove("key2");
    this.rootLogger.error("finished mdc pattern test", new NullPointerException("test"));
    final Marker marker = MarkerManager.getMarker("EVENT");
    this.rootLogger.error(marker, "marker test");
    appender.stop();
    final List<String> list = appender.getMessages();
    final String string = list.get(0);
    assertTrue("Incorrect header: " + string, string.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
    assertTrue("Incorrect footer", list.get(list.size() - 1).equals("</Events>"));
    this.checkContains("loggerFqcn=\"" + AbstractLogger.class.getName() + "\"", list);
    this.checkContains("level=\"DEBUG\"", list);
    this.checkContains(">starting mdc pattern test</Message>", list);
    // this.checkContains("<Message>starting mdc pattern test</Message>", list);
    // <Marker xmlns="" _class="org.apache.logging.log4j.MarkerManager..Log4jMarker" name="EVENT"/>
    this.checkContains("<Marker", list);
    this.checkContains("name=\"EVENT\"/>", list);
    for (final Appender app : appenders.values()) {
        this.rootLogger.addAppender(app);
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Marker(org.apache.logging.log4j.Marker) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) Test(org.junit.Test)

Example 58 with ListAppender

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

the class Rfc5424LayoutTest method testDiscardEmptyLoggerFields.

@Test
public void testDiscardEmptyLoggerFields() {
    final String mdcId = "RequestContext";
    Arrays.asList("[BAZ@32473 baz=\"org.apache.logging.log4j.core.layout.Rfc5424LayoutTest.testLoggerFields\"]" + "[RequestContext@3692 bar=\"org.apache.logging.log4j.core.layout.Rfc5424LayoutTest.testLoggerFields\"]");
    for (final Appender appender : root.getAppenders().values()) {
        root.removeAppender(appender);
    }
    final LoggerFields[] loggerFields = new LoggerFields[] { LoggerFields.createLoggerFields(new KeyValuePair[] { new KeyValuePair("dummy", Strings.EMPTY), new KeyValuePair("empty", Strings.EMPTY) }, "SD-ID", "32473", true), LoggerFields.createLoggerFields(new KeyValuePair[] { new KeyValuePair("baz", "%C.%M"), new KeyValuePair("baz", "%C.%M") }, "BAZ", "32473", false), LoggerFields.createLoggerFields(new KeyValuePair[] { new KeyValuePair("bar", "%C.%M") }, null, null, false) };
    final AbstractStringLayout layout = Rfc5424Layout.createLayout(Facility.LOCAL0, "Event", 3692, true, mdcId, null, null, true, null, "ATM", null, "key1, key2, locale", null, null, null, false, loggerFields, null);
    final ListAppender appender = new ListAppender("List", null, layout, true, false);
    appender.start();
    root.addAppender(appender);
    root.setLevel(Level.DEBUG);
    root.info("starting logger fields test");
    try {
        final List<String> list = appender.getMessages();
        assertTrue("Not enough list entries", list.size() > 0);
        final String message = list.get(0);
        Assert.assertTrue("SD-ID should have been discarded", !message.contains("SD-ID"));
        Assert.assertTrue("BAZ should have been included", message.contains("BAZ"));
        Assert.assertTrue(mdcId + "should have been included", message.contains(mdcId));
        appender.clear();
    } finally {
        root.removeAppender(appender);
        appender.stop();
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Test(org.junit.Test)

Example 59 with ListAppender

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

the class PatternLayoutMainMapLookupTest method testHeader.

@Test
public void testHeader() {
    final ListAppender listApp = context.getListAppender("List");
    final Logger logger = context.getLogger(this.getClass().getName());
    logger.info("Hello World");
    final List<String> messages = listApp.getMessages();
    Assert.assertFalse(messages.isEmpty());
    final String messagesStr = messages.toString();
    Assert.assertEquals(messagesStr, "Header: value0", messages.get(0));
    listApp.stop();
    Assert.assertEquals(messagesStr, "Footer: value1", messages.get(2));
}
Also used : ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Logger(org.apache.logging.log4j.Logger) Test(org.junit.Test)

Example 60 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.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 : 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