Search in sources :

Example 1 with StructuredDataId

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());
}
Also used : StructuredDataId(org.apache.logging.log4j.message.StructuredDataId)

Example 2 with StructuredDataId

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;
}
Also used : StructuredDataId(org.apache.logging.log4j.message.StructuredDataId) PatternParser(org.apache.logging.log4j.core.pattern.PatternParser) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) PatternFormatter(org.apache.logging.log4j.core.pattern.PatternFormatter) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 3 with StructuredDataId

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);
    }
}
Also used : StructuredDataId(org.apache.logging.log4j.message.StructuredDataId) StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) Message(org.apache.logging.log4j.message.Message) StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Aggregations

StructuredDataId (org.apache.logging.log4j.message.StructuredDataId)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 SortedMap (java.util.SortedMap)2 TreeMap (java.util.TreeMap)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 PatternFormatter (org.apache.logging.log4j.core.pattern.PatternFormatter)1 PatternParser (org.apache.logging.log4j.core.pattern.PatternParser)1 Message (org.apache.logging.log4j.message.Message)1 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)1