Search in sources :

Example 6 with MapMessage

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);
        }
    }
}
Also used : MapMessage(org.apache.logging.log4j.message.MapMessage) IndexedReadOnlyStringMap(org.apache.logging.log4j.util.IndexedReadOnlyStringMap)

Example 7 with MapMessage

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);
}
Also used : StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) MapMessage(org.apache.logging.log4j.message.MapMessage) Message(org.apache.logging.log4j.message.Message) LogEvent(org.apache.logging.log4j.core.LogEvent) MapMessage(org.apache.logging.log4j.message.MapMessage) Test(org.junit.Test)

Example 8 with MapMessage

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;
}
Also used : MapMessage(org.apache.logging.log4j.message.MapMessage) Message(org.apache.logging.log4j.message.Message) HashMap(java.util.HashMap) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) MapMessage(org.apache.logging.log4j.message.MapMessage)

Example 9 with MapMessage

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;
}
Also used : MapMessage(org.apache.logging.log4j.message.MapMessage) Message(org.apache.logging.log4j.message.Message) MapMessage(org.apache.logging.log4j.message.MapMessage) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 10 with MapMessage

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);
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) MapMessage(org.apache.logging.log4j.message.MapMessage) Test(org.junit.Test)

Aggregations

MapMessage (org.apache.logging.log4j.message.MapMessage)13 Test (org.junit.Test)8 LogEvent (org.apache.logging.log4j.core.LogEvent)6 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)6 HashMap (java.util.HashMap)5 Message (org.apache.logging.log4j.message.Message)3 Logger (org.apache.logging.log4j.Logger)2 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)2 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)2 Sampler (com.google.monitoring.runtime.instrumentation.Sampler)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 MessageConsumer (javax.jms.MessageConsumer)1 Marker (org.apache.logging.log4j.Marker)1 Filter (org.apache.logging.log4j.core.Filter)1 Configuration (org.apache.logging.log4j.core.config.Configuration)1 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)1