use of org.apache.logging.log4j.layout.template.json.JsonTemplateLayout.EventTemplateAdditionalField in project logging-log4j2 by apache.
the class JsonTemplateLayoutTest method test_event_template_additional_fields.
@Test
void test_event_template_additional_fields() {
// Create the log event.
final SimpleMessage message = new SimpleMessage("Hello, World!");
final RuntimeException exception = NonAsciiUtf8MethodNameContainingException.INSTANCE;
final Level level = Level.ERROR;
final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setLevel(level).setMessage(message).setThrown(exception).build();
// Create the event template.
final String eventTemplate = "{}";
// Create the layout.
final EventTemplateAdditionalField[] additionalFields = { EventTemplateAdditionalField.newBuilder().setKey("number").setValue("1").setFormat(EventTemplateAdditionalField.Format.JSON).build(), EventTemplateAdditionalField.newBuilder().setKey("string").setValue("foo").build(), EventTemplateAdditionalField.newBuilder().setKey("level").setValue("{\"$resolver\": \"level\", \"field\": \"name\"}").setFormat(EventTemplateAdditionalField.Format.JSON).build() };
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setStackTraceEnabled(true).setEventTemplate(eventTemplate).setEventTemplateAdditionalFields(additionalFields).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
assertThat(accessor.getInteger("number")).isEqualTo(1);
assertThat(accessor.getString("string")).isEqualTo("foo");
assertThat(accessor.getString("level")).isEqualTo(level.name());
});
}
use of org.apache.logging.log4j.layout.template.json.JsonTemplateLayout.EventTemplateAdditionalField in project logging-log4j2 by apache.
the class EventAdditionalFieldInterceptor method processTemplateBeforeResolverInjection.
@Override
public Object processTemplateBeforeResolverInjection(final EventResolverContext context, final Object node) {
// Short-circuit if there are no additional fields.
final EventTemplateAdditionalField[] additionalFields = context.getEventTemplateAdditionalFields();
if (additionalFields.length == 0) {
return node;
}
// Check that the root is an object node.
final Map<String, Object> objectNode;
try {
@SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) node;
objectNode = map;
} catch (final ClassCastException error) {
final String message = String.format("was expecting an object to merge additional fields: %s", node.getClass().getName());
throw new IllegalArgumentException(message);
}
// Merge additional fields.
for (final EventTemplateAdditionalField additionalField : additionalFields) {
final String additionalFieldKey = additionalField.getKey();
final Object additionalFieldValue;
final EventTemplateAdditionalField.Format additionalFieldFormat = additionalField.getFormat();
if (EventTemplateAdditionalField.Format.STRING.equals(additionalFieldFormat)) {
additionalFieldValue = additionalField.getValue();
} else if (EventTemplateAdditionalField.Format.JSON.equals(additionalFieldFormat)) {
try {
additionalFieldValue = JsonReader.read(additionalField.getValue());
} catch (final Exception error) {
final String message = String.format("failed reading JSON provided by additional field: %s", additionalFieldKey);
throw new IllegalArgumentException(message, error);
}
} else {
final String message = String.format("unknown format %s for additional field: %s", additionalFieldKey, additionalFieldFormat);
throw new IllegalArgumentException(message);
}
objectNode.put(additionalFieldKey, additionalFieldValue);
}
// Return the modified node.
return node;
}
Aggregations