use of org.apache.logging.log4j.layout.template.json.JsonTemplateLayout in project logging-log4j2 by apache.
the class ReadOnlyStringMapResolverTest method testReadOnlyStringMapPattern.
private static void testReadOnlyStringMapPattern(final String patternMatchedKey, final String patternMatchedValue, final String patternMismatchedKey, final LogEvent logEvent, final String resolverName) {
// Create the event template.
final String mapFieldName = "map";
final String eventTemplate = writeJson(asMap(mapFieldName, asMap("$resolver", resolverName, "pattern", patternMatchedKey)));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setStackTraceEnabled(true).setEventTemplate(eventTemplate).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
assertThat(accessor.getString(new String[] { mapFieldName, patternMatchedKey })).isEqualTo(patternMatchedValue);
assertThat(accessor.exists(new String[] { mapFieldName, patternMismatchedKey })).isFalse();
});
}
use of org.apache.logging.log4j.layout.template.json.JsonTemplateLayout in project logging-log4j2 by apache.
the class ReadOnlyStringMapResolverTest method testReadOnlyStringMapKeyAccess.
private static void testReadOnlyStringMapKeyAccess(final String directlyAccessedKey, final String directlyAccessedValue, final String directlyAccessedNullPropertyKey, final LogEvent logEvent, final String resolverName) {
// Create the event template.
String eventTemplate = writeJson(asMap(directlyAccessedKey, asMap("$resolver", resolverName, "key", directlyAccessedKey), directlyAccessedNullPropertyKey, asMap("$resolver", resolverName, "key", directlyAccessedNullPropertyKey)));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setStackTraceEnabled(true).setEventTemplate(eventTemplate).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
assertThat(accessor.getString(directlyAccessedKey)).isEqualTo(directlyAccessedValue);
assertThat(accessor.getString(directlyAccessedNullPropertyKey)).isNull();
});
}
use of org.apache.logging.log4j.layout.template.json.JsonTemplateLayout in project logging-log4j2 by apache.
the class ReadOnlyStringMapResolverTest method pattern_replacement_should_work.
@Test
void pattern_replacement_should_work() {
// Create the event template.
final String eventTemplate = writeJson(asMap("$resolver", "mdc", "pattern", "user:(role|rank)", "replacement", "$1"));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
// Create the log event.
final StringMap contextData = new SortedArrayStringMap();
contextData.putValue("user:role", "engineer");
contextData.putValue("user:rank", "senior");
final LogEvent logEvent = Log4jLogEvent.newBuilder().setContextData(contextData).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
assertThat(accessor.getString("role")).isEqualTo("engineer");
assertThat(accessor.getString("rank")).isEqualTo("senior");
});
}
use of org.apache.logging.log4j.layout.template.json.JsonTemplateLayout in project logging-log4j2 by apache.
the class RecyclerFactoriesTest method test_RecyclerFactoryConverter_using_XML_config.
@Test
@LoggerContextSource("recyclerFactoryCustomizedJsonTemplateLayoutLogging.xml")
void test_RecyclerFactoryConverter_using_XML_config(@Named(value = "List") final ListAppender appender) throws Exception {
final JsonTemplateLayout layout = (JsonTemplateLayout) appender.getLayout();
final Field field = JsonTemplateLayout.class.getDeclaredField("contextRecycler");
field.setAccessible(true);
final QueueingRecycler<?> contextRecycler = (QueueingRecycler<?>) field.get(layout);
final MpmcArrayQueue<?> queue = (MpmcArrayQueue<?>) contextRecycler.getQueue();
Assertions.assertThat(queue.capacity()).isEqualTo(512);
}
Aggregations