use of org.apache.logging.log4j.message.MapMessage in project logging-log4j2 by apache.
the class MapPatternConverter method format.
/**
* {@inheritDoc}
*/
@Override
public void format(final LogEvent event, final StringBuilder toAppendTo) {
MapMessage msg;
if (event.getMessage() instanceof MapMessage) {
msg = (MapMessage) event.getMessage();
} else {
return;
}
final IndexedReadOnlyStringMap sortedMap = msg.getIndexedReadOnlyStringMap();
// Key/Value pair for the Map in a similar format to Hashtable.toString()
if (key == null) {
if (sortedMap.isEmpty()) {
toAppendTo.append("{}");
return;
}
toAppendTo.append("{");
for (int i = 0; i < sortedMap.size(); i++) {
if (i > 0) {
toAppendTo.append(", ");
}
toAppendTo.append(sortedMap.getKeyAt(i)).append('=').append((String) sortedMap.getValueAt(i));
}
toAppendTo.append('}');
} else {
// otherwise they just want a single key output
final String val = sortedMap.getValue(key);
if (val != null) {
toAppendTo.append(val);
}
}
}
use of org.apache.logging.log4j.message.MapMessage in project logging-log4j2 by apache.
the class RewriteAppenderTest method rewriteTest.
@Test
public void rewriteTest() {
final StructuredDataMessage msg = new StructuredDataMessage("Test", "This is a test", "Service");
msg.put("Key1", "Value1");
msg.put("Key2", "Value2");
EventLogger.logEvent(msg);
final List<LogEvent> list = app.getEvents();
assertNotNull("No events generated", list);
assertTrue("Incorrect number of events. Expected 1, got " + list.size(), list.size() == 1);
final LogEvent event = list.get(0);
final Message m = event.getMessage();
assertTrue("Message is not a MapMessage", m instanceof MapMessage);
final MapMessage message = (MapMessage) m;
final Map<String, String> map = message.getData();
assertNotNull("No Map", map);
assertTrue("Incorrect number of map entries, expected 3 got " + map.size(), map.size() == 3);
final String value = map.get("Key1");
assertEquals("Apache", value);
}
use of org.apache.logging.log4j.message.MapMessage in project logging-log4j2 by apache.
the class MapRewritePolicy method rewrite.
/**
* Rewrite the event.
* @param source a logging event that may be returned or
* used to create a new logging event.
* @return The LogEvent after rewriting.
*/
@Override
public LogEvent rewrite(final LogEvent source) {
final Message msg = source.getMessage();
if (msg == null || !(msg instanceof MapMessage)) {
return source;
}
final Map<String, String> newMap = new HashMap<>(((MapMessage) msg).getData());
switch(mode) {
case Add:
{
newMap.putAll(map);
break;
}
default:
{
for (final Map.Entry<String, String> entry : map.entrySet()) {
if (newMap.containsKey(entry.getKey())) {
newMap.put(entry.getKey(), entry.getValue());
}
}
}
}
final MapMessage message = ((MapMessage) msg).newInstance(newMap);
final LogEvent result = new Log4jLogEvent.Builder(source).setMessage(message).build();
return result;
}
use of org.apache.logging.log4j.message.MapMessage in project logging-log4j2 by apache.
the class MapMessageLookup method lookup.
/**
* Looks up the value for the key using the data in the LogEvent.
* @param event The current LogEvent.
* @param key the key to be looked up, may be null
* @return The value associated with the key.
*/
@Override
public String lookup(final LogEvent event, final String key) {
final Message msg = event.getMessage();
if (msg instanceof MapMessage) {
try {
final Map<String, String> properties = ((MapMessage) msg).getData();
if (properties == null) {
return "";
}
if (key == null || key.length() == 0 || key.equals("*")) {
final StringBuilder sb = new StringBuilder("{");
boolean first = true;
for (final Map.Entry<String, String> entry : properties.entrySet()) {
if (!first) {
sb.append(", ");
}
sb.append(entry.getKey()).append("=").append(entry.getValue());
first = false;
}
sb.append("}");
return sb.toString();
}
return properties.get(key);
} catch (final Exception ex) {
LOGGER.warn(LOOKUP, "Error while getting property [{}].", key, ex);
return null;
}
}
return null;
}
use of org.apache.logging.log4j.message.MapMessage in project logging-log4j2 by apache.
the class MapPatternConverterTest method testConverterWithKey.
@Test
public void testConverterWithKey() {
final MapMessage msg = new MapMessage();
msg.put("subject", "I");
msg.put("verb", "love");
msg.put("object", "Log4j");
final MapPatternConverter converter = MapPatternConverter.newInstance(new String[] { "object" });
final LogEvent event = //
Log4jLogEvent.newBuilder().setLoggerName(//
"MyLogger").setLevel(//
Level.DEBUG).setMessage(//
msg).build();
final StringBuilder sb = new StringBuilder();
converter.format(event, sb);
final String str = sb.toString();
final String expected = "Log4j";
assertEquals(expected, str);
}
Aggregations