use of org.apache.logging.log4j.message.StructuredDataId in project logging-log4j2 by apache.
the class FlumeEvent method addStructuredData.
protected void addStructuredData(final String prefix, final Map<String, String> fields, final StructuredDataMessage msg) {
fields.put(prefix + EVENT_TYPE, msg.getType());
final StructuredDataId id = msg.getId();
fields.put(prefix + EVENT_ID, id.getName());
}
use of org.apache.logging.log4j.message.StructuredDataId in project logging-log4j2 by apache.
the class Rfc5424Layout method createFieldFormatters.
private Map<String, FieldFormatter> createFieldFormatters(final LoggerFields[] loggerFields, final Configuration config) {
final Map<String, FieldFormatter> sdIdMap = new HashMap<>(loggerFields == null ? 0 : loggerFields.length);
if (loggerFields != null) {
for (final LoggerFields loggerField : loggerFields) {
final StructuredDataId key = loggerField.getSdId() == null ? mdcSdId : loggerField.getSdId();
final Map<String, List<PatternFormatter>> sdParams = new HashMap<>();
final Map<String, String> fields = loggerField.getMap();
if (!fields.isEmpty()) {
final PatternParser fieldParser = createPatternParser(config, null);
for (final Map.Entry<String, String> entry : fields.entrySet()) {
final List<PatternFormatter> formatters = fieldParser.parse(entry.getValue());
sdParams.put(entry.getKey(), formatters);
}
final FieldFormatter fieldFormatter = new FieldFormatter(sdParams, loggerField.getDiscardIfAllFieldsAreEmpty());
sdIdMap.put(key.toString(), fieldFormatter);
}
}
}
return sdIdMap.size() > 0 ? sdIdMap : null;
}
use of org.apache.logging.log4j.message.StructuredDataId 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;
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, false);
sdElements.put(mdcSdIdStr, formattedContextMap);
}
}
if (isStructured) {
final StructuredDataMessage data = (StructuredDataMessage) message;
final Map<String, String> map = data.getData();
final StructuredDataId id = data.getId();
final String sdId = getId(id);
if (sdElements.containsKey(sdId)) {
final StructuredDataElement union = sdElements.get(id.toString());
union.union(map);
sdElements.put(sdId, union);
} else {
final StructuredDataElement formattedData = new StructuredDataElement(map, false);
sdElements.put(sdId, formattedData);
}
}
if (sdElements.isEmpty()) {
buffer.append('-');
return;
}
for (final Map.Entry<String, StructuredDataElement> entry : sdElements.entrySet()) {
formatStructuredElement(entry.getKey(), mdcPrefix, entry.getValue(), buffer, listChecker);
}
}
Aggregations