Search in sources :

Example 11 with StringMap

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

the class ContextDataDeserializer method deserialize.

@Override
public StringMap deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
    // Sanity check: verify that we got "Json Object":
    //        JsonToken tok = jp.nextToken();
    //        if (tok != JsonToken.START_OBJECT) {
    //            throw new IOException("Expected data to start with an Object");
    //        }
    final StringMap contextData = ContextDataFactory.createContextData();
    // Iterate over object fields:
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        final String fieldName = jp.getCurrentName();
        // move to value
        jp.nextToken();
        contextData.putValue(fieldName, jp.getText());
    }
    return contextData;
}
Also used : StringMap(org.apache.logging.log4j.util.StringMap)

Example 12 with StringMap

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

the class GarbageFreeSortedArrayThreadContextMap method hashCode.

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    final StringMap map = this.localMap.get();
    result = prime * result + ((map == null) ? 0 : map.hashCode());
    return result;
}
Also used : SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) StringMap(org.apache.logging.log4j.util.StringMap) ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap)

Example 13 with StringMap

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

the class RingBufferLogEventTest method testCreateMementoReturnsCopy.

@SuppressWarnings("deprecation")
@Test
public void testCreateMementoReturnsCopy() {
    final RingBufferLogEvent evt = new RingBufferLogEvent();
    final String loggerName = "logger.name";
    final Marker marker = MarkerManager.getMarker("marked man");
    final String fqcn = "f.q.c.n";
    final Level level = Level.TRACE;
    final Message data = new SimpleMessage("message");
    final Throwable t = new InternalError("not a real error");
    final ContextStack contextStack = new MutableThreadContextStack(Arrays.asList("a", "b"));
    final String threadName = "main";
    final StackTraceElement location = null;
    final long currentTimeMillis = 12345;
    final long nanoTime = 1;
    evt.setValues(null, loggerName, marker, fqcn, level, data, t, (StringMap) evt.getContextData(), contextStack, -1, threadName, -1, location, currentTimeMillis, nanoTime);
    ((StringMap) evt.getContextData()).putValue("key", "value");
    final LogEvent actual = evt.createMemento();
    assertEquals(evt.getLoggerName(), actual.getLoggerName());
    assertEquals(evt.getMarker(), actual.getMarker());
    assertEquals(evt.getLoggerFqcn(), actual.getLoggerFqcn());
    assertEquals(evt.getLevel(), actual.getLevel());
    assertEquals(evt.getMessage(), actual.getMessage());
    assertEquals(evt.getThrown(), actual.getThrown());
    assertEquals(evt.getContextMap(), actual.getContextMap());
    assertEquals(evt.getContextData(), actual.getContextData());
    assertEquals(evt.getContextStack(), actual.getContextStack());
    assertEquals(evt.getThreadName(), actual.getThreadName());
    assertEquals(evt.getTimeMillis(), actual.getTimeMillis());
    assertEquals(evt.getSource(), actual.getSource());
    assertEquals(evt.getThrownProxy(), actual.getThrownProxy());
}
Also used : StringMap(org.apache.logging.log4j.util.StringMap) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Marker(org.apache.logging.log4j.Marker) MutableThreadContextStack(org.apache.logging.log4j.spi.MutableThreadContextStack) MutableThreadContextStack(org.apache.logging.log4j.spi.MutableThreadContextStack) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack) Level(org.apache.logging.log4j.Level) Test(org.junit.Test)

Example 14 with StringMap

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

the class RingBufferLogEventTest method testSerializationDeserialization.

@Test
public void testSerializationDeserialization() throws IOException, ClassNotFoundException {
    final RingBufferLogEvent evt = new RingBufferLogEvent();
    final String loggerName = "logger.name";
    final Marker marker = null;
    final String fqcn = "f.q.c.n";
    final Level level = Level.TRACE;
    final Message data = new SimpleMessage("message");
    final Throwable t = new InternalError("not a real error");
    final ContextStack contextStack = null;
    final String threadName = "main";
    final StackTraceElement location = null;
    final long currentTimeMillis = 12345;
    final long nanoTime = 1;
    evt.setValues(null, loggerName, marker, fqcn, level, data, t, (StringMap) evt.getContextData(), contextStack, -1, threadName, -1, location, currentTimeMillis, nanoTime);
    ((StringMap) evt.getContextData()).putValue("key", "value");
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream out = new ObjectOutputStream(baos);
    out.writeObject(evt);
    final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    final RingBufferLogEvent other = (RingBufferLogEvent) in.readObject();
    assertEquals(loggerName, other.getLoggerName());
    assertEquals(marker, other.getMarker());
    assertEquals(fqcn, other.getLoggerFqcn());
    assertEquals(level, other.getLevel());
    assertEquals(data, other.getMessage());
    assertNull("null after serialization", other.getThrown());
    assertEquals(new ThrowableProxy(t), other.getThrownProxy());
    assertEquals(evt.getContextData(), other.getContextData());
    assertEquals(contextStack, other.getContextStack());
    assertEquals(threadName, other.getThreadName());
    assertEquals(location, other.getSource());
    assertEquals(currentTimeMillis, other.getTimeMillis());
}
Also used : StringMap(org.apache.logging.log4j.util.StringMap) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Marker(org.apache.logging.log4j.Marker) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MutableThreadContextStack(org.apache.logging.log4j.spi.MutableThreadContextStack) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack) ObjectOutputStream(java.io.ObjectOutputStream) ThrowableProxy(org.apache.logging.log4j.core.impl.ThrowableProxy) ByteArrayInputStream(java.io.ByteArrayInputStream) Level(org.apache.logging.log4j.Level) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 15 with StringMap

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

the class AsyncLogger method actualAsyncLog.

/**
     * This method is called by the EventHandler that processes the RingBufferLogEvent in a separate thread.
     * Merges the contents of the configuration map into the contextData, after replacing any variables in the property
     * values with the StrSubstitutor-supplied actual values.
     *
     * @param event the event to log
     */
public void actualAsyncLog(final RingBufferLogEvent event) {
    final List<Property> properties = privateConfig.loggerConfig.getPropertyList();
    if (properties != null) {
        StringMap contextData = (StringMap) event.getContextData();
        if (contextData.isFrozen()) {
            final StringMap temp = ContextDataFactory.createContextData();
            temp.putAll(contextData);
            contextData = temp;
        }
        for (int i = 0; i < properties.size(); i++) {
            final Property prop = properties.get(i);
            if (contextData.getValue(prop.getName()) != null) {
                // contextMap overrides config properties
                continue;
            }
            final String value = //
            prop.isValueNeedsLookup() ? //
            privateConfig.config.getStrSubstitutor().replace(event, prop.getValue()) : prop.getValue();
            contextData.putValue(prop.getName(), value);
        }
        event.setContextData(contextData);
    }
    final ReliabilityStrategy strategy = privateConfig.loggerConfig.getReliabilityStrategy();
    strategy.log(this, event);
}
Also used : StringMap(org.apache.logging.log4j.util.StringMap) Property(org.apache.logging.log4j.core.config.Property) ReliabilityStrategy(org.apache.logging.log4j.core.config.ReliabilityStrategy)

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