Search in sources :

Example 26 with ListAppender

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

the class Rfc5424LayoutTest method testLoggerFields.

@Test
public void testLoggerFields() {
    final String[] fields = new String[] { "[BAZ@32473 baz=\"org.apache.logging.log4j.core.layout.Rfc5424LayoutTest.testLoggerFields\"]", "[RequestContext@3692 bar=\"org.apache.logging.log4j.core.layout.Rfc5424LayoutTest.testLoggerFields\"]", "[SD-ID@32473 source=\"org.apache.logging.log4j.core.layout.Rfc5424LayoutTest.testLoggerFields\"]" };
    final List<String> expectedToContain = Arrays.asList(fields);
    for (final Appender appender : root.getAppenders().values()) {
        root.removeAppender(appender);
    }
    final LoggerFields[] loggerFields = new LoggerFields[] { LoggerFields.createLoggerFields(new KeyValuePair[] { new KeyValuePair("source", "%C.%M") }, "SD-ID", "32473", false), 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, "RequestContext", 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(list.size() > 0, "Not enough list entries");
        final String message = list.get(0);
        assertTrue(message.contains("Rfc5424LayoutTest.testLoggerFields"), "No class/method");
        for (final String value : expectedToContain) {
            assertTrue(message.contains(value), "Message expected to contain " + value + " but did not");
        }
        appender.clear();
    } finally {
        root.removeAppender(appender);
        appender.stop();
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) Test(org.junit.jupiter.api.Test)

Example 27 with ListAppender

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

the class Rfc5424LayoutTest 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
    final AbstractStringLayout layout = Rfc5424Layout.createLayout(Facility.LOCAL0, "Event", 3692, true, "RequestContext", null, null, true, null, "ATM", null, "key1, key2, locale", null, "loginId", null, true, null, null);
    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);
    ThreadContext.put("loginId", "JohnDoe");
    // 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("ipAddress", "192.168.0.120");
    ThreadContext.put("locale", Locale.US.getDisplayName());
    try {
        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);
        List<String> list = appender.getMessages();
        assertTrue(list.get(0).endsWith(line1), "Expected line 1 to end with: " + line1 + " Actual " + list.get(0));
        assertTrue(list.get(1).endsWith(line2), "Expected line 2 to end with: " + line2 + " Actual " + list.get(1));
        assertTrue(list.get(2).endsWith(line3), "Expected line 3 to end with: " + line3 + " Actual " + list.get(2));
        assertTrue(list.get(3).endsWith(line4), "Expected line 4 to end with: " + line4 + " Actual " + list.get(3));
        for (final String frame : list) {
            int length = -1;
            final int frameLength = frame.length();
            final int firstSpacePosition = frame.indexOf(' ');
            final String messageLength = frame.substring(0, firstSpacePosition);
            try {
                length = Integer.parseInt(messageLength);
                // the ListAppender removes the ending newline, so we expect one less size
                assertEquals(frameLength, messageLength.length() + length);
            } catch (final NumberFormatException e) {
                fail("Not a valid RFC 5425 frame");
            }
        }
        appender.clear();
        ThreadContext.remove("loginId");
        root.debug("This is a test");
        list = appender.getMessages();
        assertTrue(list.isEmpty(), "No messages expected, found " + list.size());
    } finally {
        root.removeAppender(appender);
        appender.stop();
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) Test(org.junit.jupiter.api.Test)

Example 28 with ListAppender

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

the class Rfc5424LayoutTest method testException.

/**
 * Test case for MDC exception conversion pattern.
 */
@Test
public void testException() throws Exception {
    for (final Appender appender : root.getAppenders().values()) {
        root.removeAppender(appender);
    }
    // set up layout/appender
    final AbstractStringLayout layout = Rfc5424Layout.createLayout(Facility.LOCAL0, "Event", 3692, true, "RequestContext", null, null, true, null, "ATM", null, "key1, key2, locale", null, "loginId", "%xEx", true, null, null);
    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);
    ThreadContext.put("loginId", "JohnDoe");
    // output starting message
    root.debug("starting mdc pattern test", new IllegalArgumentException("Test"));
    try {
        final List<String> list = appender.getMessages();
        assertTrue(list.size() > 1, "Not enough list entries");
        final String string = list.get(1);
        assertTrue(string.contains("IllegalArgumentException"), "No Exception in " + string);
        appender.clear();
    } finally {
        root.removeAppender(appender);
        appender.stop();
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) Test(org.junit.jupiter.api.Test)

Example 29 with ListAppender

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

the class Rfc5424LayoutTest method testSubstituteStructuredData.

@Test
public void testSubstituteStructuredData() {
    final String mdcId = "RequestContext";
    final String expectedToContain = String.format("ATM %s MSG-ID - Message", PROCESSID);
    for (final Appender appender : root.getAppenders().values()) {
        root.removeAppender(appender);
    }
    final AbstractStringLayout layout = Rfc5424Layout.createLayout(Facility.LOCAL0, "Event", 3692, false, mdcId, null, null, true, null, "ATM", "MSG-ID", "key1, key2, locale", null, null, null, false, null, null);
    final ListAppender appender = new ListAppender("List", null, layout, true, false);
    appender.start();
    root.addAppender(appender);
    root.setLevel(Level.DEBUG);
    root.info("Message");
    try {
        final List<String> list = appender.getMessages();
        assertTrue(list.size() > 0, "Not enough list entries");
        final String message = list.get(0);
        assertTrue(message.contains(expectedToContain), "Not the expected message received");
        appender.clear();
    } finally {
        root.removeAppender(appender);
        appender.stop();
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) Test(org.junit.jupiter.api.Test)

Example 30 with ListAppender

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

the class Rfc5424LayoutTest method testMDCLoggerFields.

/**
 * Test case for MDC logger field inclusion.
 */
@Test
public void testMDCLoggerFields() throws Exception {
    for (final Appender appender : root.getAppenders().values()) {
        root.removeAppender(appender);
    }
    final LoggerFields[] loggerFields = new LoggerFields[] { LoggerFields.createLoggerFields(new KeyValuePair[] { new KeyValuePair("source", "%C.%M") }, null, null, false), LoggerFields.createLoggerFields(new KeyValuePair[] { new KeyValuePair("source2", "%C.%M") }, null, null, false) };
    // set up layout/appender
    final AbstractStringLayout layout = Rfc5424Layout.createLayout(Facility.LOCAL0, "Event", 3692, true, "RequestContext", null, null, true, null, "ATM", null, "key1, key2, locale", null, null, null, true, loggerFields, null);
    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.info("starting logger fields test");
    try {
        final List<String> list = appender.getMessages();
        assertTrue(list.size() > 0, "Not enough list entries");
        assertTrue(list.get(0).contains("Rfc5424LayoutTest.testMDCLoggerFields"), "No class/method");
        appender.clear();
    } finally {
        root.removeAppender(appender);
        appender.stop();
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) Test(org.junit.jupiter.api.Test)

Aggregations

ListAppender (org.apache.logging.log4j.core.test.appender.ListAppender)60 Test (org.junit.Test)24 Appender (org.apache.logging.log4j.core.Appender)21 Test (org.junit.jupiter.api.Test)15 Configuration (org.apache.logging.log4j.core.config.Configuration)6 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)5 AbstractLogger (org.apache.logging.log4j.spi.AbstractLogger)5 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)4 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)4 AbstractJacksonLayout (org.apache.logging.log4j.jackson.AbstractJacksonLayout)4 Marker (org.apache.logging.log4j.Marker)3 LogEvent (org.apache.logging.log4j.core.LogEvent)3 Logger (org.apache.logging.log4j.core.Logger)3 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)3 ExtendedLogger (org.apache.logging.log4j.spi.ExtendedLogger)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 Logger (java.util.logging.Logger)2 Log (org.apache.commons.logging.Log)2 LoggerContext (org.apache.logging.log4j.core.LoggerContext)2 RoutingAppender (org.apache.logging.log4j.core.appender.routing.RoutingAppender)2