Search in sources :

Example 16 with StructuredDataMessage

use of org.apache.logging.log4j.message.StructuredDataMessage in project logging-log4j2 by apache.

the class JsonRoutingAppender2Test method routingTest.

@Test
public void routingTest() {
    StructuredDataMessage msg = new StructuredDataMessage("Test", "This is a test", "Service");
    EventLogger.logEvent(msg);
    final List<LogEvent> list = loggerContextRule.getListAppender("List").getEvents();
    assertNotNull("No events generated", list);
    assertTrue("Incorrect number of events. Expected 1, got " + list.size(), list.size() == 1);
    msg = new StructuredDataMessage("Test", "This is a test", "Unknown");
    EventLogger.logEvent(msg);
    final File file = new File(LOG_FILENAME);
    assertTrue("File was not created", file.exists());
}
Also used : StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) LogEvent(org.apache.logging.log4j.core.LogEvent) File(java.io.File) Test(org.junit.Test)

Example 17 with StructuredDataMessage

use of org.apache.logging.log4j.message.StructuredDataMessage in project logging-log4j2 by apache.

the class PropertiesRoutingAppenderTest method routingTest.

@Test
public void routingTest() {
    StructuredDataMessage msg = new StructuredDataMessage("Test", "This is a test", "Service");
    EventLogger.logEvent(msg);
    final List<LogEvent> list = app.getEvents();
    assertNotNull("No events generated", list);
    assertTrue("Incorrect number of events. Expected 1, got " + list.size(), list.size() == 1);
    msg = new StructuredDataMessage("Test", "This is a test", "Alert");
    EventLogger.logEvent(msg);
    File file = new File(ALERT_LOG_FILE);
    assertTrue("Alert file was not created", file.exists());
    msg = new StructuredDataMessage("Test", "This is a test", "Activity");
    EventLogger.logEvent(msg);
    file = new File(ACTIVITY_LOG_FILE);
    assertTrue("Activity file was not created", file.exists());
}
Also used : StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) LogEvent(org.apache.logging.log4j.core.LogEvent) File(java.io.File) Test(org.junit.Test)

Example 18 with StructuredDataMessage

use of org.apache.logging.log4j.message.StructuredDataMessage in project logging-log4j2 by apache.

the class Rfc5424LayoutTest method testEscape.

/**
     * Test case for escaping newlines and other SD PARAM-NAME special characters.
     */
@Test
public void testEscape() 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, "#012", "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("escaped", "Testing escaping \n \" ] \"");
    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("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: " + lineEscaped3 + " Actual " + list.get(2), list.get(2).endsWith(lineEscaped3));
        assertTrue("Expected line 4 to end with: " + lineEscaped4 + " Actual " + list.get(3), list.get(3).endsWith(lineEscaped4));
        appender.clear();
        ThreadContext.remove("loginId");
        root.debug("This is a test");
        list = appender.getMessages();
        assertTrue("No messages expected, found " + list.size(), list.isEmpty());
    } finally {
        root.removeAppender(appender);
        appender.stop();
    }
}
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 19 with StructuredDataMessage

use of org.apache.logging.log4j.message.StructuredDataMessage 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("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));
        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) {
                assertTrue("Not a valid RFC 5425 frame", false);
            }
        }
        appender.clear();
        ThreadContext.remove("loginId");
        root.debug("This is a test");
        list = appender.getMessages();
        assertTrue("No messages expected, found " + list.size(), list.isEmpty());
    } finally {
        root.removeAppender(appender);
        appender.stop();
    }
}
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 20 with StructuredDataMessage

use of org.apache.logging.log4j.message.StructuredDataMessage in project logging-log4j2 by apache.

the class JsonRoutingAppenderTest method routingTest.

@Test
public void routingTest() {
    StructuredDataMessage msg = new StructuredDataMessage("Test", "This is a test", "Service");
    EventLogger.logEvent(msg);
    final List<LogEvent> list = loggerContextRule.getListAppender("List").getEvents();
    assertNotNull("No events generated", list);
    assertTrue("Incorrect number of events. Expected 1, got " + list.size(), list.size() == 1);
    msg = new StructuredDataMessage("Test", "This is a test", "Unknown");
    EventLogger.logEvent(msg);
    final File file = new File(LOG_FILENAME);
    assertTrue("File was not created", file.exists());
}
Also used : StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) LogEvent(org.apache.logging.log4j.core.LogEvent) File(java.io.File) Test(org.junit.Test)

Aggregations

StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)40 Test (org.junit.Test)32 Event (org.apache.flume.Event)12 AvroFlumeEvent (org.apache.flume.source.avro.AvroFlumeEvent)11 LogEvent (org.apache.logging.log4j.core.LogEvent)11 File (java.io.File)6 Message (org.apache.logging.log4j.message.Message)6 EventLogger (org.apache.logging.log4j.EventLogger)5 Logger (org.apache.logging.log4j.Logger)4 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)4 StatusLogger (org.apache.logging.log4j.status.StatusLogger)4 Appender (org.apache.logging.log4j.core.Appender)3 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)3 Map (java.util.Map)2 Marker (org.apache.logging.log4j.Marker)2 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)2 MapMessage (org.apache.logging.log4j.message.MapMessage)2 HashMap (java.util.HashMap)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1