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"));
}
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"));
}
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());
}
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"));
}
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
}
Aggregations