Search in sources :

Example 6 with StringMapMessage

use of org.apache.logging.log4j.message.StringMapMessage in project logging-log4j2 by apache.

the class InterpolatorTest method testInterpolatorMapMessageWithMapPrefix.

@Test
public void testInterpolatorMapMessageWithMapPrefix() {
    final HashMap<String, String> configProperties = new HashMap<>();
    configProperties.put("key", "configProperties");
    Interpolator interpolator = new Interpolator(configProperties);
    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();
    assertEquals("mapMessage", interpolator.lookup(event, "map:key"));
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) HashMap(java.util.HashMap) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) Test(org.junit.jupiter.api.Test)

Example 7 with StringMapMessage

use of org.apache.logging.log4j.message.StringMapMessage in project logging-log4j2 by apache.

the class InterpolatorTest method testInterpolatorMapMessageWithNoPrefix.

@Test
public void testInterpolatorMapMessageWithNoPrefix() {
    final HashMap<String, String> configProperties = new HashMap<>();
    configProperties.put("key", "configProperties");
    Interpolator interpolator = new Interpolator(configProperties);
    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();
    assertEquals("configProperties", interpolator.lookup(event, "key"));
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) HashMap(java.util.HashMap) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) Test(org.junit.jupiter.api.Test)

Example 8 with StringMapMessage

use of org.apache.logging.log4j.message.StringMapMessage in project logging-log4j2 by apache.

the class GelfLayout3Test method mapMessage.

@Test
public void mapMessage(final LoggerContext context, @Named final ListAppender list) throws IOException {
    list.clear();
    final Logger logger = context.getLogger(getClass());
    ThreadContext.put("loginId", "rgoers");
    ThreadContext.put("internalId", "12345");
    StringMapMessage message = new StringMapMessage();
    message.put("arg1", "test1");
    message.put("arg2", "");
    message.put("arg3", "test3");
    message.put("message", "My Test Message");
    logger.info(message);
    final String gelf = list.getMessages().get(0);
    final ObjectMapper mapper = new ObjectMapper();
    final JsonNode json = mapper.readTree(gelf);
    assertEquals("arg1=\"test1\" arg2=\"\" arg3=\"test3\" message=\"My Test Message\"", json.get("short_message").asText());
    assertEquals("myhost", json.get("host").asText());
    assertNotNull(json.get("_mdc.loginId"));
    assertEquals("rgoers", json.get("_mdc.loginId").asText());
    assertNull(json.get("_mdc.internalId"));
    assertNull(json.get("_mdc.requestId"));
    String msg = json.get("full_message").asText();
    assertTrue(msg.contains("loginId=rgoers"));
    assertTrue(msg.contains("GelfLayout3Test"));
    assertTrue(msg.contains("arg1=\"test1\""));
    assertNull(json.get("_map.arg2"));
    assertEquals("test1", json.get("_map.arg1").asText());
    assertEquals("test3", json.get("_map.arg3").asText());
    assertNull(json.get("_empty"));
    assertEquals("FOO", json.get("_foo").asText());
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) JsonNode(com.fasterxml.jackson.databind.JsonNode) Logger(org.apache.logging.log4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 9 with StringMapMessage

use of org.apache.logging.log4j.message.StringMapMessage in project logging-log4j2 by apache.

the class MapLookupTest method testEventStringMapMessage.

@Test
public void testEventStringMapMessage() {
    final HashMap<String, String> map = new HashMap<>();
    map.put("A", "B");
    final HashMap<String, String> eventMap = new HashMap<>();
    eventMap.put("A1", "B1");
    final StringMapMessage message = new StringMapMessage(eventMap);
    final LogEvent event = Log4jLogEvent.newBuilder().setMessage(message).build();
    final MapLookup lookup = new MapLookup(map);
    assertEquals("B", lookup.lookup(event, "A"));
    assertEquals("B1", lookup.lookup(event, "A1"));
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) HashMap(java.util.HashMap) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) Test(org.junit.jupiter.api.Test)

Example 10 with StringMapMessage

use of org.apache.logging.log4j.message.StringMapMessage in project logging-log4j2 by apache.

the class CollectionLoggingTest method testSimpleMap.

@Test
@ResourceLock(value = Resources.SYSTEM_PROPERTIES, mode = ResourceAccessMode.READ)
public void testSimpleMap(final LoggerContext context) {
    final Logger logger = context.getLogger(CollectionLoggingTest.class.getName());
    logger.error(System.getProperties());
    final Map<String, String> map = new HashMap<>();
    map.put("MyKey1", "MyValue1");
    map.put("MyKey2", "MyValue2");
    logger.error(new StringMapMessage(map));
    logger.error(map);
// TODO: some assertions
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) HashMap(java.util.HashMap) Logger(org.apache.logging.log4j.Logger) Test(org.junit.jupiter.api.Test) ResourceLock(org.junit.jupiter.api.parallel.ResourceLock)

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