use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class JsonTemplateLayoutTest method test_PatternResolver.
@Test
void test_PatternResolver() {
// Create the event template.
final String eventTemplate = writeJson(asMap("message", asMap("$resolver", "pattern", "pattern", "%p:%m")));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
// Create the log event.
final SimpleMessage message = new SimpleMessage("foo");
final Level level = Level.FATAL;
final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setMessage(message).setLevel(level).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
final String expectedMessage = String.format("%s:%s", level, message.getFormattedMessage());
assertThat(accessor.getString("message")).isEqualTo(expectedMessage);
});
}
use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class JsonTemplateLayoutTest method test_stringified_exception_resolver_with_maxStringLength.
@Test
void test_stringified_exception_resolver_with_maxStringLength() {
// Create the event template.
final String eventTemplate = writeJson(asMap("stackTrace", asMap("$resolver", "exception", "field", "stackTrace", "stringified", true)));
// Create the layout.
final int maxStringLength = eventTemplate.length();
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).setMaxStringLength(maxStringLength).setStackTraceEnabled(true).build();
// Create the log event.
final SimpleMessage message = new SimpleMessage("foo");
final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setMessage(message).setThrown(NonAsciiUtf8MethodNameContainingException.INSTANCE).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
final int expectedLength = maxStringLength + JsonTemplateLayoutDefaults.getTruncatedStringSuffix().length();
assertThat(accessor.getString("stackTrace").length()).isEqualTo(expectedLength);
});
}
use of org.apache.logging.log4j.message.SimpleMessage 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.message.SimpleMessage in project logging-log4j2 by apache.
the class JsonTemplateLayoutTest method test_root_cause.
@Test
void test_root_cause() {
// Create the log event.
final SimpleMessage message = new SimpleMessage("Hello, World!");
final RuntimeException exceptionCause = new RuntimeException("failure cause for test purposes");
final RuntimeException exception = new RuntimeException("failure for test purposes", exceptionCause);
final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setLevel(Level.ERROR).setMessage(message).setThrown(exception).build();
// Create the event template.
final String eventTemplate = writeJson(asMap("ex_class", asMap("$resolver", "exception", "field", "className"), "ex_message", asMap("$resolver", "exception", "field", "message"), "ex_stacktrace", asMap("$resolver", "exception", "field", "stackTrace", "stringified", true), "root_ex_class", asMap("$resolver", "exceptionRootCause", "field", "className"), "root_ex_message", asMap("$resolver", "exceptionRootCause", "field", "message"), "root_ex_stacktrace", asMap("$resolver", "exceptionRootCause", "field", "stackTrace", "stackTrace", asMap("stringified", true))));
// 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("ex_class")).isEqualTo(exception.getClass().getCanonicalName());
assertThat(accessor.getString("ex_message")).isEqualTo(exception.getMessage());
assertThat(accessor.getString("ex_stacktrace")).startsWith(exception.getClass().getCanonicalName() + ": " + exception.getMessage());
assertThat(accessor.getString("root_ex_class")).isEqualTo(exceptionCause.getClass().getCanonicalName());
assertThat(accessor.getString("root_ex_message")).isEqualTo(exceptionCause.getMessage());
assertThat(accessor.getString("root_ex_stacktrace")).startsWith(exceptionCause.getClass().getCanonicalName() + ": " + exceptionCause.getMessage());
});
}
use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class JsonTemplateLayoutTest method test_StackTraceElement_template.
@Test
void test_StackTraceElement_template() {
// Create the stack trace element template.
final String classNameFieldName = "className";
final String methodNameFieldName = "methodName";
final String fileNameFieldName = "fileName";
final String lineNumberFieldName = "lineNumber";
final String stackTraceElementTemplate = writeJson(asMap(classNameFieldName, asMap("$resolver", "stackTraceElement", "field", "className"), methodNameFieldName, asMap("$resolver", "stackTraceElement", "field", "methodName"), fileNameFieldName, asMap("$resolver", "stackTraceElement", "field", "fileName"), lineNumberFieldName, asMap("$resolver", "stackTraceElement", "field", "lineNumber")));
// Create the event template.
final String stackTraceFieldName = "stackTrace";
final String eventTemplate = writeJson(asMap(stackTraceFieldName, asMap("$resolver", "exception", "field", "stackTrace")));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setStackTraceEnabled(true).setStackTraceElementTemplate(stackTraceElementTemplate).setEventTemplate(eventTemplate).build();
// Create the log event.
final SimpleMessage message = new SimpleMessage("Hello, World!");
final RuntimeException exceptionCause = new RuntimeException("failure cause for test purposes");
final RuntimeException exception = new RuntimeException("failure for test purposes", exceptionCause);
final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setLevel(Level.ERROR).setMessage(message).setThrown(exception).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
assertThat(accessor.exists(stackTraceFieldName)).isTrue();
@SuppressWarnings("unchecked") final List<Map<String, Object>> deserializedStackTraceElements = accessor.getObject(stackTraceFieldName, List.class);
final StackTraceElement[] stackTraceElements = exception.getStackTrace();
assertThat(deserializedStackTraceElements.size()).isEqualTo(stackTraceElements.length);
for (int stackTraceElementIndex = 0; stackTraceElementIndex < stackTraceElements.length; stackTraceElementIndex++) {
final StackTraceElement stackTraceElement = stackTraceElements[stackTraceElementIndex];
final Map<String, Object> deserializedStackTraceElement = deserializedStackTraceElements.get(stackTraceElementIndex);
assertThat(deserializedStackTraceElement.size()).isEqualTo(4);
assertThat(deserializedStackTraceElement.get(classNameFieldName)).isEqualTo(stackTraceElement.getClassName());
assertThat(deserializedStackTraceElement.get(methodNameFieldName)).isEqualTo(stackTraceElement.getMethodName());
assertThat(deserializedStackTraceElement.get(fileNameFieldName)).isEqualTo(stackTraceElement.getFileName());
assertThat(deserializedStackTraceElement.get(lineNumberFieldName)).isEqualTo(stackTraceElement.getLineNumber());
}
});
}
Aggregations