Search in sources :

Example 1 with StringMapMessage

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") };
}
Also used : MutableThreadContextStack(org.apache.logging.log4j.spi.MutableThreadContextStack) ThreadContextStack(org.apache.logging.log4j.spi.ThreadContextStack) StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) MutableThreadContextStack(org.apache.logging.log4j.spi.MutableThreadContextStack) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 2 with StringMapMessage

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"));
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) Message(org.apache.logging.log4j.message.Message) StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) JsonTemplateLayout(org.apache.logging.log4j.layout.template.json.JsonTemplateLayout) Test(org.junit.jupiter.api.Test)

Example 3 with StringMapMessage

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");
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) Test(org.junit.jupiter.api.Test)

Example 4 with StringMapMessage

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));
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) Test(org.junit.jupiter.api.Test)

Example 5 with StringMapMessage

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");
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) Filter(org.apache.logging.log4j.core.Filter) HashMap(java.util.HashMap) Collection(java.util.Collection) IndexedReadOnlyStringMap(org.apache.logging.log4j.util.IndexedReadOnlyStringMap) Logger(org.apache.logging.log4j.Logger) Test(org.junit.jupiter.api.Test) LoggerContextSource(org.apache.logging.log4j.core.test.junit.LoggerContextSource)

Aggregations

StringMapMessage (org.apache.logging.log4j.message.StringMapMessage)25 Test (org.junit.jupiter.api.Test)23 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)21 LogEvent (org.apache.logging.log4j.core.LogEvent)20 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)8 HashMap (java.util.HashMap)7 Message (org.apache.logging.log4j.message.Message)7 JsonTemplateLayout (org.apache.logging.log4j.layout.template.json.JsonTemplateLayout)5 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)4 Logger (org.apache.logging.log4j.Logger)3 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)3 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Collection (java.util.Collection)1 Filter (org.apache.logging.log4j.core.Filter)1 LoggerContextSource (org.apache.logging.log4j.core.test.junit.LoggerContextSource)1 MapMessageLookup (org.apache.logging.log4j.lookup.MapMessageLookup)1 MapMessage (org.apache.logging.log4j.message.MapMessage)1 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)1