Search in sources :

Example 1 with StructuredDataCollectionMessage

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

the class Rfc5424LayoutTest method testCollection.

/**
 * Test case for MDC conversion pattern.
 */
@Test
public void testCollection() 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");
    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");
        final StructuredDataMessage msg2 = new StructuredDataMessage("Extra@18060", null, "Audit");
        msg2.put("Item1", "Hello");
        msg2.put("Item2", "World");
        List<StructuredDataMessage> messages = new ArrayList<>();
        messages.add(msg);
        messages.add(msg2);
        final StructuredDataCollectionMessage collectionMessage = new StructuredDataCollectionMessage(messages);
        root.info(MarkerManager.getMarker("EVENT"), collectionMessage);
        List<String> list = appender.getMessages();
        String result = list.get(0);
        assertTrue(result.contains(collectionLine1), "Expected line to contain " + collectionLine1 + ", Actual " + result);
        assertTrue(result.contains(collectionLine2), "Expected line to contain " + collectionLine2 + ", Actual " + result);
        assertTrue(result.contains(collectionLine3), "Expected line to contain " + collectionLine3 + ", Actual " + result);
        assertTrue(result.endsWith(collectionEndOfLine), "Expected line to end with: " + collectionEndOfLine + " Actual " + result);
        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();
    } 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) ArrayList(java.util.ArrayList) StructuredDataCollectionMessage(org.apache.logging.log4j.message.StructuredDataCollectionMessage) Test(org.junit.jupiter.api.Test)

Example 2 with StructuredDataCollectionMessage

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

the class Rfc5424Layout method appendStructuredElements.

private void appendStructuredElements(final StringBuilder buffer, final LogEvent event) {
    final Message message = event.getMessage();
    final boolean isStructured = message instanceof StructuredDataMessage || message instanceof StructuredDataCollectionMessage;
    if (!isStructured && (fieldFormatters != null && fieldFormatters.isEmpty()) && !includeMdc) {
        buffer.append('-');
        return;
    }
    final Map<String, StructuredDataElement> sdElements = new HashMap<>();
    final Map<String, String> contextMap = event.getContextData().toMap();
    if (mdcRequired != null) {
        checkRequired(contextMap);
    }
    if (fieldFormatters != null) {
        for (final Map.Entry<String, FieldFormatter> sdElement : fieldFormatters.entrySet()) {
            final String sdId = sdElement.getKey();
            final StructuredDataElement elem = sdElement.getValue().format(event);
            sdElements.put(sdId, elem);
        }
    }
    if (includeMdc && contextMap.size() > 0) {
        final String mdcSdIdStr = mdcSdId.toString();
        final StructuredDataElement union = sdElements.get(mdcSdIdStr);
        if (union != null) {
            union.union(contextMap);
            sdElements.put(mdcSdIdStr, union);
        } else {
            final StructuredDataElement formattedContextMap = new StructuredDataElement(contextMap, mdcPrefix, false);
            sdElements.put(mdcSdIdStr, formattedContextMap);
        }
    }
    if (isStructured) {
        if (message instanceof MessageCollectionMessage) {
            for (final StructuredDataMessage data : ((StructuredDataCollectionMessage) message)) {
                addStructuredData(sdElements, data);
            }
        } else {
            addStructuredData(sdElements, (StructuredDataMessage) message);
        }
    }
    if (sdElements.isEmpty()) {
        buffer.append('-');
        return;
    }
    for (final Map.Entry<String, StructuredDataElement> entry : sdElements.entrySet()) {
        formatStructuredElement(entry.getKey(), entry.getValue(), buffer, listChecker);
    }
}
Also used : StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) MessageCollectionMessage(org.apache.logging.log4j.message.MessageCollectionMessage) Message(org.apache.logging.log4j.message.Message) StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) StructuredDataCollectionMessage(org.apache.logging.log4j.message.StructuredDataCollectionMessage) HashMap(java.util.HashMap) MessageCollectionMessage(org.apache.logging.log4j.message.MessageCollectionMessage) StructuredDataCollectionMessage(org.apache.logging.log4j.message.StructuredDataCollectionMessage) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Aggregations

StructuredDataCollectionMessage (org.apache.logging.log4j.message.StructuredDataCollectionMessage)2 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 Appender (org.apache.logging.log4j.core.Appender)1 ListAppender (org.apache.logging.log4j.core.test.appender.ListAppender)1 Message (org.apache.logging.log4j.message.Message)1 MessageCollectionMessage (org.apache.logging.log4j.message.MessageCollectionMessage)1 Test (org.junit.jupiter.api.Test)1