Search in sources :

Example 21 with StringMap

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

the class CopyOnWriteSortedArrayThreadContextMap method putAllValues.

@Override
public <V> void putAllValues(final Map<String, V> values) {
    if (values == null || values.isEmpty()) {
        return;
    }
    StringMap map = localMap.get();
    map = map == null ? createStringMap() : createStringMap(map);
    for (final Map.Entry<String, V> entry : values.entrySet()) {
        map.putValue(entry.getKey(), entry.getValue());
    }
    map.freeze();
    localMap.set(map);
}
Also used : SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) StringMap(org.apache.logging.log4j.util.StringMap) ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) Map(java.util.Map) StringMap(org.apache.logging.log4j.util.StringMap) HashMap(java.util.HashMap) ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap)

Example 22 with StringMap

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

the class CopyOnWriteSortedArrayThreadContextMap method putAll.

@Override
public void putAll(final Map<String, String> values) {
    if (values == null || values.isEmpty()) {
        return;
    }
    StringMap map = localMap.get();
    map = map == null ? createStringMap() : createStringMap(map);
    for (final Map.Entry<String, String> entry : values.entrySet()) {
        map.putValue(entry.getKey(), entry.getValue());
    }
    map.freeze();
    localMap.set(map);
}
Also used : SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) StringMap(org.apache.logging.log4j.util.StringMap) ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) Map(java.util.Map) StringMap(org.apache.logging.log4j.util.StringMap) HashMap(java.util.HashMap) ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap)

Example 23 with StringMap

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

the class CopyOnWriteSortedArrayThreadContextMap method createThreadLocalMap.

// LOG4J2-479: by default, use a plain ThreadLocal, only use InheritableThreadLocal if configured.
// (This method is package protected for JUnit tests.)
private ThreadLocal<StringMap> createThreadLocalMap() {
    final PropertiesUtil managerProps = PropertiesUtil.getProperties();
    final boolean inheritable = managerProps.getBooleanProperty(INHERITABLE_MAP);
    if (inheritable) {
        return new InheritableThreadLocal<StringMap>() {

            @Override
            protected StringMap childValue(final StringMap parentValue) {
                return parentValue != null ? createStringMap(parentValue) : null;
            }
        };
    }
    // if not inheritable, return plain ThreadLocal with null as initial value
    return new ThreadLocal<>();
}
Also used : SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) StringMap(org.apache.logging.log4j.util.StringMap) ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap) PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil)

Example 24 with StringMap

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

the class ContextDataAsEntryListDeserializer method deserialize.

@Override
public StringMap deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
    final List<MapEntry> list = jp.readValueAs(new TypeReference<List<MapEntry>>() {
    });
    final StringMap contextData = new ContextDataFactory().createContextData();
    for (final MapEntry mapEntry : list) {
        contextData.putValue(mapEntry.getKey(), mapEntry.getValue());
    }
    return contextData;
}
Also used : StringMap(org.apache.logging.log4j.util.StringMap) ContextDataFactory(org.apache.logging.log4j.core.impl.ContextDataFactory) List(java.util.List)

Example 25 with StringMap

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

the class Log4jLogEventTest method testBuilderCorrectlyCopiesAllEventAttributesInclContextData.

@Test
public void testBuilderCorrectlyCopiesAllEventAttributesInclContextData() {
    final StringMap contextData = new SortedArrayStringMap();
    contextData.putValue("A", "B");
    final ContextStack contextStack = ThreadContext.getImmutableStack();
    final Exception exception = new Exception("test");
    final Marker marker = MarkerManager.getMarker("EVENTTEST");
    final Message message = new SimpleMessage("foo");
    final StackTraceElement stackTraceElement = new StackTraceElement("A", "B", "file", 123);
    final String fqcn = "qualified";
    final String name = "Ceci n'est pas une pipe";
    final String threadName = "threadName";
    final Log4jLogEvent event = //
    Log4jLogEvent.newBuilder().setContextData(//
    contextData).setContextStack(//
    contextStack).setEndOfBatch(//
    true).setIncludeLocation(//
    true).setLevel(//
    Level.FATAL).setLoggerFqcn(//
    fqcn).setLoggerName(//
    name).setMarker(//
    marker).setMessage(//
    message).setNanoTime(//
    1234567890L).setSource(//
    stackTraceElement).setThreadName(//
    threadName).setThrown(//
    exception).setTimeMillis(987654321L).build();
    assertSame(contextData, event.getContextData());
    assertSame(contextStack, event.getContextStack());
    assertEquals(true, event.isEndOfBatch());
    assertEquals(true, event.isIncludeLocation());
    assertSame(Level.FATAL, event.getLevel());
    assertSame(fqcn, event.getLoggerFqcn());
    assertSame(name, event.getLoggerName());
    assertSame(marker, event.getMarker());
    assertSame(message, event.getMessage());
    assertEquals(1234567890L, event.getNanoTime());
    assertSame(stackTraceElement, event.getSource());
    assertSame(threadName, event.getThreadName());
    assertSame(exception, event.getThrown());
    assertEquals(987654321L, event.getTimeMillis());
    final LogEvent event2 = new Log4jLogEvent.Builder(event).build();
    assertEquals("copy constructor builder", event2, event);
    assertEquals("same hashCode", event2.hashCode(), event.hashCode());
}
Also used : StringMap(org.apache.logging.log4j.util.StringMap) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ReusableObjectMessage(org.apache.logging.log4j.message.ReusableObjectMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) ReusableMessage(org.apache.logging.log4j.message.ReusableMessage) Message(org.apache.logging.log4j.message.Message) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) Marker(org.apache.logging.log4j.Marker) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack) IOException(java.io.IOException) Test(org.junit.Test) ClockFactoryTest(org.apache.logging.log4j.core.util.ClockFactoryTest)

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