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);
}
}
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);
}
}
Aggregations