use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class JsonTemplateLayoutTest method test_unresolvable_nested_fields_are_skipped.
@Test
void test_unresolvable_nested_fields_are_skipped() {
// Create the event template.
final String eventTemplate = writeJson(asMap("exception", asMap("message", asMap("$resolver", "exception", "field", "message"), "className", asMap("$resolver", "exception", "field", "className")), "exceptionRootCause", asMap("message", asMap("$resolver", "exceptionRootCause", "field", "message"), "className", asMap("$resolver", "exceptionRootCause", "field", "className")), "source", asMap("lineNumber", asMap("$resolver", "source", "field", "lineNumber"), "fileName", asMap("$resolver", "source", "field", "fileName")), "emptyMap", Collections.emptyMap(), "emptyList", Collections.emptyList(), "null", null));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).setStackTraceEnabled(// Disable "exception" and "exceptionRootCause" resolvers.
false).setLocationInfoEnabled(// Disable the "source" resolver.
false).build();
// Create the log event.
final SimpleMessage message = new SimpleMessage("foo");
final Exception thrown = new RuntimeException("bar");
final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setMessage(message).setThrown(thrown).build();
// Check the serialized event.
final String expectedSerializedLogEventJson = "{}" + JsonTemplateLayoutDefaults.getEventDelimiter();
final String actualSerializedLogEventJson = layout.toSerializable(logEvent);
Assertions.assertThat(actualSerializedLogEventJson).isEqualTo(expectedSerializedLogEventJson);
}
use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class JsonTemplateLayoutTest method test_marker_name.
@Test
void test_marker_name() {
// Create the log event.
final SimpleMessage message = new SimpleMessage("Hello, World!");
final String markerName = "test";
final Marker marker = MarkerManager.getMarker(markerName);
final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setLevel(Level.ERROR).setMessage(message).setMarker(marker).build();
// Create the event template.
final String messageKey = "message";
final String markerNameKey = "marker";
final String eventTemplate = writeJson(asMap("message", asMap("$resolver", "message"), "marker", asMap("$resolver", "marker", "field", "name")));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
assertThat(accessor.getString(messageKey)).isEqualTo(message.getFormattedMessage());
assertThat(accessor.getString(markerNameKey)).isEqualTo(markerName);
});
}
use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class JsonTemplateLayoutTest method test_null_eventDelimiter.
@Test
void test_null_eventDelimiter() {
// Create the event template.
final String eventTemplate = writeJson(asMap("key", "val"));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).setEventDelimiter("\0").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.
final String serializedLogEvent = layout.toSerializable(logEvent);
assertThat(serializedLogEvent).isEqualTo(eventTemplate + '\0');
}
use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class MessageResolverTest method test_message_fallbackKey.
@Test
void test_message_fallbackKey() {
// Create the event template.
final String eventTemplate = writeJson(asMap("message", asMap("$resolver", "message", "fallbackKey", "formattedMessage")));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
// Create a log event with a MapMessage.
final Message mapMessage = new StringMapMessage().with("key1", "val1");
final LogEvent mapMessageLogEvent = Log4jLogEvent.newBuilder().setMessage(mapMessage).setTimeMillis(System.currentTimeMillis()).build();
// Check the serialized MapMessage.
usingSerializedLogEventAccessor(layout, mapMessageLogEvent, accessor -> assertThat(accessor.getString(new String[] { "message", "key1" })).isEqualTo("val1"));
// Create a log event with a SimpleMessage.
final Message simpleMessage = new SimpleMessage("simple");
final LogEvent simpleMessageLogEvent = Log4jLogEvent.newBuilder().setMessage(simpleMessage).setTimeMillis(System.currentTimeMillis()).build();
// Check the serialized MapMessage.
usingSerializedLogEventAccessor(layout, simpleMessageLogEvent, accessor -> assertThat(accessor.getString(new String[] { "message", "formattedMessage" })).isEqualTo("simple"));
}
use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class ReadOnlyStringMapResolverTest method test_mdc_flatten.
@Test
void test_mdc_flatten() {
// Create the log event.
final SimpleMessage message = new SimpleMessage("Hello, World!");
final StringMap contextData = new SortedArrayStringMap();
final String mdcPatternMatchedKey = "mdcKey1";
final String mdcPatternMatchedValue = "mdcValue1";
contextData.putValue(mdcPatternMatchedKey, mdcPatternMatchedValue);
final String mdcPatternMismatchedKey = "mdcKey2";
final String mdcPatternMismatchedValue = "mdcValue2";
contextData.putValue(mdcPatternMismatchedKey, mdcPatternMismatchedValue);
final LogEvent logEvent = Log4jLogEvent.newBuilder().setMessage(message).setContextData(contextData).build();
// Check the serialized event.
testReadOnlyStringMapFlatten(mdcPatternMatchedKey, mdcPatternMatchedValue, mdcPatternMismatchedKey, logEvent, "mdc");
}
Aggregations