use of org.apache.logging.log4j.layout.template.json.JsonTemplateLayout in project logging-log4j2 by apache.
the class CounterResolverTest method verify.
private static void verify(final String eventTemplate, final Object expectedNumber1, final Object expectedNumber2) {
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
// Create the log event.
final LogEvent logEvent = Log4jLogEvent.newBuilder().build();
// Check the 1st serialized event.
final String serializedLogEvent1 = layout.toSerializable(logEvent);
final Object deserializedLogEvent1 = JsonReader.read(serializedLogEvent1);
assertThat(deserializedLogEvent1).isEqualTo(expectedNumber1);
// Check the 2nd serialized event.
final String serializedLogEvent2 = layout.toSerializable(logEvent);
final Object deserializedLogEvent2 = JsonReader.read(serializedLogEvent2);
assertThat(deserializedLogEvent2).isEqualTo(expectedNumber2);
}
use of org.apache.logging.log4j.layout.template.json.JsonTemplateLayout 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.layout.template.json.JsonTemplateLayout in project logging-log4j2 by apache.
the class MessageResolverTest method test_ObjectMessage.
@Test
void test_ObjectMessage() {
// Create the log event.
final int id = 0xDEADBEEF;
final String name = "name-" + id;
final Object attachment = new LinkedHashMap<String, Object>() {
{
put("id", id);
put("name", name);
}
};
final ObjectMessage message = new ObjectMessage(attachment);
final LogEvent logEvent = Log4jLogEvent.newBuilder().setMessage(message).build();
// Create the event template.
final String eventTemplate = writeJson(asMap("message", asMap("$resolver", "message")));
// Create the layout.
JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setStackTraceEnabled(true).setEventTemplate(eventTemplate).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
assertThat(accessor.getInteger(new String[] { "message", "id" })).isEqualTo(id);
assertThat(accessor.getString(new String[] { "message", "name" })).isEqualTo(name);
});
}
use of org.apache.logging.log4j.layout.template.json.JsonTemplateLayout in project logging-log4j2 by apache.
the class CaseConverterResolverTest method test_errorHandlingStrategy.
@ParameterizedTest
@CsvSource({ // failure message | locale | input | strategy | replacement | output
"" + ",nl" + ",1" + ",pass" + ",null" + ",1", "" + ",nl" + ",[2]" + ",pass" + ",null" + ",[2]", "was expecting a string value" + ",nl" + ",1" + ",fail" + ",null" + ",null", "" + ",nl" + ",1" + ",replace" + ",null" + ",null", "" + ",nl" + ",1" + ",replace" + ",2" + ",2", "" + ",nl" + ",1" + ",replace" + ",\"s\"" + ",\"s\"" })
void test_errorHandlingStrategy(final String failureMessage, final String locale, final String inputJson, final String errorHandlingStrategy, final String replacementJson, final String outputJson) {
// Parse arguments.
final Object input = readJson(inputJson);
final Object replacement = readJson(replacementJson);
final Object output = readJson(outputJson);
// Create the event template.
final String eventTemplate = writeJson(asMap("output", asMap("$resolver", "caseConverter", "case", "lower", "locale", locale, "input", input, "errorHandlingStrategy", errorHandlingStrategy, "replacement", replacement)));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
// Create the log event.
final LogEvent logEvent = Log4jLogEvent.newBuilder().build();
// Check the serialized event.
final boolean failureExpected = Strings.isNotBlank(failureMessage);
if (failureExpected) {
assertThatThrownBy(() -> layout.toSerializable(logEvent)).hasMessageContaining(failureMessage);
} else {
usingSerializedLogEventAccessor(layout, logEvent, accessor -> assertThat(accessor.getObject("output")).isEqualTo(output));
}
}
use of org.apache.logging.log4j.layout.template.json.JsonTemplateLayout in project logging-log4j2 by apache.
the class CaseConverterResolverTest method test_upper.
@ParameterizedTest
@CsvSource({ // case | locale | input | output
"upper" + ",nl" + ",ioz" + ",IOZ", "upper" + ",nl" + ",IOZ" + ",IOZ", "lower" + ",nl" + ",ioz" + ",ioz", "lower" + ",nl" + ",IOZ" + ",ioz", "upper" + ",tr" + ",ıiğüşöç" + ",IİĞÜŞÖÇ", "upper" + ",tr" + ",IİĞÜŞÖÇ" + ",IİĞÜŞÖÇ", "lower" + ",tr" + ",ıiğüşöç" + ",ıiğüşöç", "lower" + ",tr" + ",IİĞÜŞÖÇ" + ",ıiğüşöç" })
void test_upper(final String case_, final String locale, final String input, final String output) {
// Create the event template.
final String eventTemplate = writeJson(asMap("output", asMap("$resolver", "caseConverter", "case", case_, "locale", locale, "input", asMap("$resolver", "mdc", "key", "input"))));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
// Create the log event.
final StringMap contextData = new SortedArrayStringMap();
contextData.putValue("input", input);
final LogEvent logEvent = Log4jLogEvent.newBuilder().setContextData(contextData).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> assertThat(accessor.getString("output")).isEqualTo(output));
}
Aggregations