Search in sources :

Example 1 with MessageCollectionMessage

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

the class Rfc5424Layout method appendMessage.

private void appendMessage(final StringBuilder buffer, final LogEvent event) {
    final Message message = event.getMessage();
    // This layout formats StructuredDataMessages instead of delegating to the Message itself.
    final String text = (message instanceof StructuredDataMessage || message instanceof MessageCollectionMessage) ? message.getFormat() : message.getFormattedMessage();
    if (text != null && text.length() > 0) {
        buffer.append(' ').append(escapeNewlines(text, escapeNewLine));
    }
    if (exceptionFormatters != null && event.getThrown() != null) {
        final StringBuilder exception = new StringBuilder(LF);
        for (final PatternFormatter formatter : exceptionFormatters) {
            formatter.format(event, exception);
        }
        buffer.append(escapeNewlines(exception.toString(), escapeNewLine));
    }
    if (includeNewLine) {
        buffer.append(LF);
    }
}
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) PatternFormatter(org.apache.logging.log4j.core.pattern.PatternFormatter) MessageCollectionMessage(org.apache.logging.log4j.message.MessageCollectionMessage)

Example 2 with MessageCollectionMessage

use of org.apache.logging.log4j.message.MessageCollectionMessage 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

Message (org.apache.logging.log4j.message.Message)2 MessageCollectionMessage (org.apache.logging.log4j.message.MessageCollectionMessage)2 StructuredDataCollectionMessage (org.apache.logging.log4j.message.StructuredDataCollectionMessage)2 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 PatternFormatter (org.apache.logging.log4j.core.pattern.PatternFormatter)1