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;
}
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;
}
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());
}
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());
}
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);
}
Aggregations