Search in sources :

Example 1 with ReadOnlyStringMap

use of org.apache.logging.log4j.util.ReadOnlyStringMap in project logging-log4j2 by apache.

the class ContextDataJsonAttributeConverterTest method testConvert01.

@Test
public void testConvert01() {
    final StringMap map = new SortedArrayStringMap();
    map.putValue("test1", "another1");
    map.putValue("key2", "value2");
    final String converted = this.converter.convertToDatabaseColumn(map);
    assertNotNull("The converted value should not be null.", converted);
    final ReadOnlyStringMap reversed = this.converter.convertToEntityAttribute(converted);
    assertNotNull("The reversed value should not be null.", reversed);
    assertEquals("The reversed value is not correct.", map, reversed);
}
Also used : SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) StringMap(org.apache.logging.log4j.util.StringMap) ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap) ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) Test(org.junit.Test)

Example 2 with ReadOnlyStringMap

use of org.apache.logging.log4j.util.ReadOnlyStringMap in project logging-log4j2 by apache.

the class ContextDataJsonAttributeConverter method convertToEntityAttribute.

@Override
public ReadOnlyStringMap convertToEntityAttribute(final String s) {
    if (Strings.isEmpty(s)) {
        return null;
    }
    try {
        final StringMap result = ContextDataFactory.createContextData();
        final ObjectNode root = (ObjectNode) OBJECT_MAPPER.readTree(s);
        final Iterator<Map.Entry<String, JsonNode>> entries = root.fields();
        while (entries.hasNext()) {
            final Map.Entry<String, JsonNode> entry = entries.next();
            // Don't know what to do with non-text values.
            // Maybe users who need this need to provide custom converter?
            final Object value = entry.getValue().textValue();
            result.putValue(entry.getKey(), value);
        }
        return result;
    } catch (final IOException e) {
        throw new PersistenceException("Failed to convert JSON string to map.", e);
    }
}
Also used : ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap) StringMap(org.apache.logging.log4j.util.StringMap) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) PersistenceException(javax.persistence.PersistenceException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap) Map(java.util.Map) StringMap(org.apache.logging.log4j.util.StringMap)

Example 3 with ReadOnlyStringMap

use of org.apache.logging.log4j.util.ReadOnlyStringMap in project logging-log4j2 by apache.

the class NoSqlDatabaseManager method writeInternal.

@Override
protected void writeInternal(final LogEvent event) {
    if (!this.isRunning() || this.connection == null || this.connection.isClosed()) {
        throw new AppenderLoggingException("Cannot write logging event; NoSQL manager not connected to the database.");
    }
    final NoSqlObject<W> entity = this.connection.createObject();
    entity.set("level", event.getLevel());
    entity.set("loggerName", event.getLoggerName());
    entity.set("message", event.getMessage() == null ? null : event.getMessage().getFormattedMessage());
    final StackTraceElement source = event.getSource();
    if (source == null) {
        entity.set("source", (Object) null);
    } else {
        entity.set("source", this.convertStackTraceElement(source));
    }
    final Marker marker = event.getMarker();
    if (marker == null) {
        entity.set("marker", (Object) null);
    } else {
        entity.set("marker", buildMarkerEntity(marker));
    }
    entity.set("threadId", event.getThreadId());
    entity.set("threadName", event.getThreadName());
    entity.set("threadPriority", event.getThreadPriority());
    entity.set("millis", event.getTimeMillis());
    entity.set("date", new java.util.Date(event.getTimeMillis()));
    @SuppressWarnings("ThrowableResultOfMethodCallIgnored") Throwable thrown = event.getThrown();
    if (thrown == null) {
        entity.set("thrown", (Object) null);
    } else {
        final NoSqlObject<W> originalExceptionEntity = this.connection.createObject();
        NoSqlObject<W> exceptionEntity = originalExceptionEntity;
        exceptionEntity.set("type", thrown.getClass().getName());
        exceptionEntity.set("message", thrown.getMessage());
        exceptionEntity.set("stackTrace", this.convertStackTrace(thrown.getStackTrace()));
        while (thrown.getCause() != null) {
            thrown = thrown.getCause();
            final NoSqlObject<W> causingExceptionEntity = this.connection.createObject();
            causingExceptionEntity.set("type", thrown.getClass().getName());
            causingExceptionEntity.set("message", thrown.getMessage());
            causingExceptionEntity.set("stackTrace", this.convertStackTrace(thrown.getStackTrace()));
            exceptionEntity.set("cause", causingExceptionEntity);
            exceptionEntity = causingExceptionEntity;
        }
        entity.set("thrown", originalExceptionEntity);
    }
    final ReadOnlyStringMap contextMap = event.getContextData();
    if (contextMap == null) {
        entity.set("contextMap", (Object) null);
    } else {
        final NoSqlObject<W> contextMapEntity = this.connection.createObject();
        contextMap.forEach(new BiConsumer<String, String>() {

            @Override
            public void accept(final String key, final String val) {
                contextMapEntity.set(key, val);
            }
        });
        entity.set("contextMap", contextMapEntity);
    }
    final ThreadContext.ContextStack contextStack = event.getContextStack();
    if (contextStack == null) {
        entity.set("contextStack", (Object) null);
    } else {
        entity.set("contextStack", contextStack.asList().toArray());
    }
    this.connection.insertObject(entity);
}
Also used : AppenderLoggingException(org.apache.logging.log4j.core.appender.AppenderLoggingException) ThreadContext(org.apache.logging.log4j.ThreadContext) Marker(org.apache.logging.log4j.Marker) ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap)

Example 4 with ReadOnlyStringMap

use of org.apache.logging.log4j.util.ReadOnlyStringMap in project logging-log4j2 by apache.

the class ThreadContextMapFilter method filter.

private Result filter() {
    boolean match = false;
    if (useMap) {
        ReadOnlyStringMap currentContextData = null;
        final IndexedReadOnlyStringMap map = getStringMap();
        for (int i = 0; i < map.size(); i++) {
            if (currentContextData == null) {
                currentContextData = currentContextData();
            }
            final String toMatch = currentContextData.getValue(map.getKeyAt(i));
            match = toMatch != null && ((List<String>) map.getValueAt(i)).contains(toMatch);
            if ((!isAnd() && match) || (isAnd() && !match)) {
                break;
            }
        }
    } else {
        match = value.equals(currentContextData().getValue(key));
    }
    return match ? onMatch : onMismatch;
}
Also used : IndexedReadOnlyStringMap(org.apache.logging.log4j.util.IndexedReadOnlyStringMap) ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap) ArrayList(java.util.ArrayList) List(java.util.List) IndexedReadOnlyStringMap(org.apache.logging.log4j.util.IndexedReadOnlyStringMap)

Example 5 with ReadOnlyStringMap

use of org.apache.logging.log4j.util.ReadOnlyStringMap in project logging-log4j2 by apache.

the class OpenHashStringMap method equals.

@Override
public boolean equals(final Object obj) {
    if (obj == this) {
        return true;
    }
    if (!(obj instanceof ReadOnlyStringMap)) {
        return false;
    }
    final ReadOnlyStringMap other = (ReadOnlyStringMap) obj;
    if (other.size() != size()) {
        return false;
    }
    int pos = arraySize;
    if (containsNullKey) {
        if (!Objects.equals(getObjectValue(null), other.getValue(null))) {
            return false;
        }
    }
    --pos;
    final K[] myKeys = this.keys;
    for (; pos >= 0; pos--) {
        K k;
        if ((k = myKeys[pos]) != null) {
            if (!Objects.equals(values[pos], other.getValue((String) k))) {
                return false;
            }
        }
    }
    return true;
}
Also used : ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap)

Aggregations

ReadOnlyStringMap (org.apache.logging.log4j.util.ReadOnlyStringMap)7 StringMap (org.apache.logging.log4j.util.StringMap)3 SortedArrayStringMap (org.apache.logging.log4j.util.SortedArrayStringMap)2 Test (org.junit.Test)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 PersistenceException (javax.persistence.PersistenceException)1 Marker (org.apache.logging.log4j.Marker)1 ThreadContext (org.apache.logging.log4j.ThreadContext)1 AppenderLoggingException (org.apache.logging.log4j.core.appender.AppenderLoggingException)1 IndexedReadOnlyStringMap (org.apache.logging.log4j.util.IndexedReadOnlyStringMap)1