use of org.apache.logging.log4j.message.StringMapMessage in project logging-log4j2 by apache.
the class MapRewritePolicyTest method setupClass.
@BeforeAll
public static void setupClass() {
stringMap.putValue("test1", "one");
stringMap.putValue("test2", "two");
map = stringMap.toMap();
logEvent0 = //
Log4jLogEvent.newBuilder().setLoggerName(//
"test").setContextData(//
stringMap).setLoggerFqcn(//
"MapRewritePolicyTest.setupClass()").setLevel(//
Level.ERROR).setMessage(//
new SimpleMessage("Test")).setThrown(//
new RuntimeException("test")).setThreadName("none").setSource(new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 28)).setTimeMillis(2).build();
logEvent1 = //
((Log4jLogEvent) logEvent0).asBuilder().setMessage(//
new StringMapMessage(map)).setSource(//
new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 29)).build();
final ThreadContextStack stack = new MutableThreadContextStack(new ArrayList<>(map.values()));
logEvent2 = //
((Log4jLogEvent) logEvent0).asBuilder().setContextStack(//
stack).setMarker(//
MarkerManager.getMarker("test")).setLevel(//
Level.TRACE).setMessage(//
new StructuredDataMessage("test", "Nothing", "test", map)).setTimeMillis(//
20000000).setSource(//
new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 30)).build();
logEvent3 = //
((Log4jLogEvent) logEvent0).asBuilder().setContextStack(//
stack).setLevel(//
Level.ALL).setMessage(//
new StringMapMessage(map)).setTimeMillis(//
Long.MAX_VALUE).setSource(//
new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 31)).build();
rewrite = new KeyValuePair[] { new KeyValuePair("test2", "2"), new KeyValuePair("test3", "three") };
}
use of org.apache.logging.log4j.message.StringMapMessage 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.StringMapMessage in project logging-log4j2 by apache.
the class ReadOnlyStringMapResolverTest method test_map_key_access.
@Test
public void test_map_key_access() {
// Create the log event.
final String directlyAccessedKey = "mapKey1";
final String directlyAccessedValue = "mapValue1";
final String directlyAccessedNullPropertyKey = "mapKey2";
final Message message = new StringMapMessage().with(directlyAccessedKey, directlyAccessedValue);
final LogEvent logEvent = Log4jLogEvent.newBuilder().setMessage(message).build();
// Check the serialized event.
testReadOnlyStringMapKeyAccess(directlyAccessedKey, directlyAccessedValue, directlyAccessedNullPropertyKey, logEvent, "map");
}
use of org.apache.logging.log4j.message.StringMapMessage in project logging-log4j2 by apache.
the class MapFilterTest method testFilter.
@Test
public void testFilter() {
final KeyValuePair[] pairs = new KeyValuePair[] { new KeyValuePair("FromAccount", "211000"), new KeyValuePair("ToAccount", "123456") };
MapFilter filter = MapFilter.createFilter(pairs, "and", null, null);
assertNotNull(filter);
filter.start();
StringMapMessage msg = new StringMapMessage();
msg.put("ToAccount", "123456");
msg.put("FromAccount", "211000");
msg.put("Amount", "1000.00");
assertTrue(filter.isStarted());
assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.DEBUG, null, msg, null));
msg.put("ToAccount", "111111");
assertSame(Filter.Result.DENY, filter.filter(null, Level.ERROR, null, msg, null));
filter = MapFilter.createFilter(pairs, "or", null, null);
assertNotNull(filter);
filter.start();
msg = new StringMapMessage();
msg.put("ToAccount", "123456");
msg.put("FromAccount", "211000");
msg.put("Amount", "1000.00");
assertTrue(filter.isStarted());
assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.DEBUG, null, msg, null));
msg.put("ToAccount", "111111");
assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.ERROR, null, msg, null));
}
use of org.apache.logging.log4j.message.StringMapMessage in project logging-log4j2 by apache.
the class MapFilterTest method testConfig.
@Test
@LoggerContextSource("log4j2-mapfilter.xml")
public void testConfig(final Configuration config, @Named("LIST") final ListAppender app) {
final Filter filter = config.getFilter();
assertNotNull(filter, "No MapFilter");
assertTrue(filter instanceof MapFilter, "Not a MapFilter");
final MapFilter mapFilter = (MapFilter) filter;
assertFalse(mapFilter.isAnd(), "Should not be And filter");
final IndexedReadOnlyStringMap map = mapFilter.getStringMap();
assertNotNull(map, "No Map");
assertFalse(map.isEmpty(), "No elements in Map");
assertEquals(1, map.size(), "Incorrect number of elements in Map");
assertTrue(map.containsKey("eventId"), "Map does not contain key eventId");
assertEquals(2, map.<Collection<?>>getValue("eventId").size(), "List does not contain 2 elements");
final Logger logger = LogManager.getLogger(MapFilterTest.class);
final Map<String, String> eventMap = new HashMap<>();
eventMap.put("eventId", "Login");
logger.debug(new StringMapMessage(eventMap));
final List<String> msgs = app.getMessages();
assertNotNull(msgs, "No messages");
assertFalse(msgs.isEmpty(), "No messages");
}
Aggregations