Search in sources :

Example 6 with StringMap

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

the class ContextDataAttributeConverterTest method testConvertToDatabaseColumn02.

@Test
public void testConvertToDatabaseColumn02() {
    final StringMap map = new SortedArrayStringMap();
    map.putValue("someKey", "coolValue");
    map.putValue("anotherKey", "testValue");
    map.putValue("myKey", "yourValue");
    assertEquals("The converted value is not correct.", map.toString(), this.converter.convertToDatabaseColumn(map));
}
Also used : SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) StringMap(org.apache.logging.log4j.util.StringMap) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) Test(org.junit.Test)

Example 7 with StringMap

use of org.apache.logging.log4j.util.StringMap 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 8 with StringMap

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

the class MDCContextMap method getReadOnlyContextData.

@Override
public StringMap getReadOnlyContextData() {
    final Map<String, String> copy = getCopy();
    if (copy.isEmpty()) {
        return EMPTY_CONTEXT_DATA;
    }
    final StringMap result = new SortedArrayStringMap();
    for (final Entry<String, String> entry : copy.entrySet()) {
        result.putValue(entry.getKey(), entry.getValue());
    }
    return result;
}
Also used : SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) StringMap(org.apache.logging.log4j.util.StringMap) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap)

Example 9 with StringMap

use of org.apache.logging.log4j.util.StringMap 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 10 with StringMap

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

the class MutableLogEventTest method createContextData.

private static StringMap createContextData() {
    final StringMap result = new SortedArrayStringMap();
    result.putValue("a", "1");
    result.putValue("b", "2");
    return result;
}
Also used : SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) StringMap(org.apache.logging.log4j.util.StringMap) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap)

Aggregations

StringMap (org.apache.logging.log4j.util.StringMap)26 SortedArrayStringMap (org.apache.logging.log4j.util.SortedArrayStringMap)19 ReadOnlyStringMap (org.apache.logging.log4j.util.ReadOnlyStringMap)14 Test (org.junit.Test)8 Marker (org.apache.logging.log4j.Marker)5 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)5 Message (org.apache.logging.log4j.message.Message)5 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)4 IOException (java.io.IOException)3 Map (java.util.Map)3 Level (org.apache.logging.log4j.Level)3 LogEvent (org.apache.logging.log4j.core.LogEvent)3 ReusableMessage (org.apache.logging.log4j.message.ReusableMessage)3 HashMap (java.util.HashMap)2 ClockFactoryTest (org.apache.logging.log4j.core.util.ClockFactoryTest)2 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)2 ReusableObjectMessage (org.apache.logging.log4j.message.ReusableObjectMessage)2 MutableThreadContextStack (org.apache.logging.log4j.spi.MutableThreadContextStack)2 PropertiesUtil (org.apache.logging.log4j.util.PropertiesUtil)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1