use of org.apache.logging.log4j.message.StringMapMessage in project logging-log4j2 by apache.
the class InterpolatorTest method testInterpolatorMapMessageWithNoPrefixConfigDoesntMatch.
@Test
public void testInterpolatorMapMessageWithNoPrefixConfigDoesntMatch() {
Interpolator interpolator = new Interpolator(Collections.emptyMap());
final HashMap<String, String> map = new HashMap<>();
map.put("key", "mapMessage");
LogEvent event = Log4jLogEvent.newBuilder().setLoggerName(getClass().getName()).setLoggerFqcn(Logger.class.getName()).setLevel(Level.INFO).setMessage(new StringMapMessage(map)).build();
assertNull(interpolator.lookup(event, "key"), "Values without a map prefix should not match MapMessages");
}
use of org.apache.logging.log4j.message.StringMapMessage in project logging-log4j2 by apache.
the class MapMessageLookupTest method testStringMapMessageLookup.
@Test
public void testStringMapMessageLookup() {
// GIVEN: A StringMapMessage object
final Map<String, String> values = new HashMap<>(3);
values.put("A", "a");
values.put("B", "b");
values.put("C", "c");
final MapMessage message = new StringMapMessage(values);
// AND: An event with that message
final LogEvent event = Log4jLogEvent.newBuilder().setLevel(Level.DEBUG).setMessage(message).build();
// AND: A MapMessageLookup object
final MapMessageLookup lookup = new MapMessageLookup();
// WHEN: Lookup is performed
final String a = lookup.lookup(event, "A");
final String b = lookup.lookup(event, "B");
final String c = lookup.lookup(event, "C");
// THEN: The looked up values are correct
assertEquals("a", a);
assertEquals("b", b);
assertEquals("c", c);
}
use of org.apache.logging.log4j.message.StringMapMessage in project logging-log4j2 by apache.
the class MessagePatternConverterTest method testMapMessageFormatJson.
@Test
public void testMapMessageFormatJson() {
final MessagePatternConverter converter = MessagePatternConverter.newInstance(null, new String[] { "json" });
Message msg = new StringMapMessage().with("key", "val");
LogEvent event = //
Log4jLogEvent.newBuilder().setLoggerName(//
"MyLogger").setLevel(//
Level.DEBUG).setMessage(msg).build();
StringBuilder sb = new StringBuilder();
converter.format(event, sb);
assertEquals("{\"key\":\"val\"}", sb.toString(), "Unexpected result");
}
use of org.apache.logging.log4j.message.StringMapMessage in project logging-log4j2 by apache.
the class MessageResolverTest method test_StringMapMessage.
@Test
void test_StringMapMessage() {
// Create the log event.
final StringMapMessage message = new StringMapMessage();
message.put("message", "Hello, World!");
message.put("bottle", "Kickapoo Joy Juice");
final LogEvent logEvent = Log4jLogEvent.newBuilder().setMessage(message).build();
// Create the event template.
final String eventTemplate = writeJson(asMap("message", asMap("$resolver", "message")));
// 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[] { "message", "message" })).isEqualTo("Hello, World!");
assertThat(accessor.getString(new String[] { "message", "bottle" })).isEqualTo("Kickapoo Joy Juice");
});
}
use of org.apache.logging.log4j.message.StringMapMessage in project logging-log4j2 by apache.
the class MessageResolverTest method test_MapMessage_serialization.
@Test
void test_MapMessage_serialization() {
// Create the event template.
final String eventTemplate = writeJson(asMap("message", asMap("$resolver", "message")));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
// Create the log event with a MapMessage.
final StringMapMessage mapMessage = new StringMapMessage().with("key1", "val1").with("key2", 0xDEADBEEF).with("key3", Collections.singletonMap("key3.1", "val3.1"));
final LogEvent logEvent = Log4jLogEvent.newBuilder().setMessage(mapMessage).setTimeMillis(System.currentTimeMillis()).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
assertThat(accessor.getString(new String[] { "message", "key1" })).isEqualTo("val1");
assertThat(accessor.getInteger(new String[] { "message", "key2" })).isEqualTo(0xDEADBEEF);
assertThat(accessor.getString(new String[] { "message", "key3", "key3.1" })).isEqualTo("val3.1");
});
}
Aggregations