Search in sources :

Example 46 with Message

use of org.apache.logging.log4j.message.Message 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 47 with Message

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

the class MessageAttributeConverterTest method testConvert01.

@Test
public void testConvert01() {
    final Message message = LOGGER.getMessageFactory().newMessage("Message #{} said [{}].", 3, "Hello");
    final String converted = this.converter.convertToDatabaseColumn(message);
    assertNotNull("The converted value should not be null.", converted);
    assertEquals("The converted value is not correct.", "Message #3 said [Hello].", converted);
    final Message reversed = this.converter.convertToEntityAttribute(converted);
    assertNotNull("The reversed value should not be null.", reversed);
    assertEquals("The reversed value is not correct.", "Message #3 said [Hello].", reversed.getFormattedMessage());
}
Also used : Message(org.apache.logging.log4j.message.Message) Test(org.junit.Test)

Example 48 with Message

use of org.apache.logging.log4j.message.Message 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 49 with Message

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

the class GelfLayout method toText.

private StringBuilder toText(final LogEvent event, final StringBuilder builder, final boolean gcFree) {
    builder.append('{');
    builder.append("\"version\":\"1.1\",");
    builder.append("\"host\":\"");
    JsonUtils.quoteAsString(toNullSafeString(host), builder);
    builder.append(QC);
    builder.append("\"timestamp\":").append(formatTimestamp(event.getTimeMillis())).append(C);
    builder.append("\"level\":").append(formatLevel(event.getLevel())).append(C);
    if (event.getThreadName() != null) {
        builder.append("\"_thread\":\"");
        JsonUtils.quoteAsString(event.getThreadName(), builder);
        builder.append(QC);
    }
    if (event.getLoggerName() != null) {
        builder.append("\"_logger\":\"");
        JsonUtils.quoteAsString(event.getLoggerName(), builder);
        builder.append(QC);
    }
    if (additionalFields.length > 0) {
        final StrSubstitutor strSubstitutor = getConfiguration().getStrSubstitutor();
        for (final KeyValuePair additionalField : additionalFields) {
            builder.append(QU);
            JsonUtils.quoteAsString(additionalField.getKey(), builder);
            builder.append("\":\"");
            final String value = valueNeedsLookup(additionalField.getValue()) ? strSubstitutor.replace(event, additionalField.getValue()) : additionalField.getValue();
            JsonUtils.quoteAsString(toNullSafeString(value), builder);
            builder.append(QC);
        }
    }
    if (includeThreadContext) {
        event.getContextData().forEach(WRITE_KEY_VALUES_INTO, builder);
    }
    if (event.getThrown() != null) {
        builder.append("\"full_message\":\"");
        if (includeStacktrace) {
            JsonUtils.quoteAsString(formatThrowable(event.getThrown()), builder);
        } else {
            JsonUtils.quoteAsString(event.getThrown().toString(), builder);
        }
        builder.append(QC);
    }
    builder.append("\"short_message\":\"");
    final Message message = event.getMessage();
    if (message instanceof CharSequence) {
        JsonUtils.quoteAsString(((CharSequence) message), builder);
    } else if (gcFree && message instanceof StringBuilderFormattable) {
        final StringBuilder messageBuffer = getMessageStringBuilder();
        try {
            ((StringBuilderFormattable) message).formatTo(messageBuffer);
            JsonUtils.quoteAsString(messageBuffer, builder);
        } finally {
            trimToMaxSize(messageBuffer);
        }
    } else {
        JsonUtils.quoteAsString(toNullSafeString(message.getFormattedMessage()), builder);
    }
    builder.append(Q);
    builder.append('}');
    if (includeNullDelimiter) {
        builder.append('\0');
    }
    return builder;
}
Also used : StrSubstitutor(org.apache.logging.log4j.core.lookup.StrSubstitutor) KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) Message(org.apache.logging.log4j.message.Message) StringBuilderFormattable(org.apache.logging.log4j.util.StringBuilderFormattable)

Example 50 with Message

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

the class Rfc5424Layout method appendStructuredElements.

private void appendStructuredElements(final StringBuilder buffer, final LogEvent event) {
    final Message message = event.getMessage();
    final boolean isStructured = message instanceof StructuredDataMessage;
    if (!isStructured && (fieldFormatters != null && fieldFormatters.isEmpty()) && !includeMdc) {
        buffer.append('-');
        return;
    }
    final Map<String, StructuredDataElement> sdElements = new HashMap<>();
    final Map<String, String> contextMap = event.getContextData().toMap();
    if (mdcRequired != null) {
        checkRequired(contextMap);
    }
    if (fieldFormatters != null) {
        for (final Map.Entry<String, FieldFormatter> sdElement : fieldFormatters.entrySet()) {
            final String sdId = sdElement.getKey();
            final StructuredDataElement elem = sdElement.getValue().format(event);
            sdElements.put(sdId, elem);
        }
    }
    if (includeMdc && contextMap.size() > 0) {
        final String mdcSdIdStr = mdcSdId.toString();
        final StructuredDataElement union = sdElements.get(mdcSdIdStr);
        if (union != null) {
            union.union(contextMap);
            sdElements.put(mdcSdIdStr, union);
        } else {
            final StructuredDataElement formattedContextMap = new StructuredDataElement(contextMap, false);
            sdElements.put(mdcSdIdStr, formattedContextMap);
        }
    }
    if (isStructured) {
        final StructuredDataMessage data = (StructuredDataMessage) message;
        final Map<String, String> map = data.getData();
        final StructuredDataId id = data.getId();
        final String sdId = getId(id);
        if (sdElements.containsKey(sdId)) {
            final StructuredDataElement union = sdElements.get(id.toString());
            union.union(map);
            sdElements.put(sdId, union);
        } else {
            final StructuredDataElement formattedData = new StructuredDataElement(map, false);
            sdElements.put(sdId, formattedData);
        }
    }
    if (sdElements.isEmpty()) {
        buffer.append('-');
        return;
    }
    for (final Map.Entry<String, StructuredDataElement> entry : sdElements.entrySet()) {
        formatStructuredElement(entry.getKey(), mdcPrefix, entry.getValue(), buffer, listChecker);
    }
}
Also used : StructuredDataId(org.apache.logging.log4j.message.StructuredDataId) StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) Message(org.apache.logging.log4j.message.Message) StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Aggregations

Message (org.apache.logging.log4j.message.Message)80 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)45 Test (org.junit.Test)42 LogEvent (org.apache.logging.log4j.core.LogEvent)33 Marker (org.apache.logging.log4j.Marker)23 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)22 Level (org.apache.logging.log4j.Level)17 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)14 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)14 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)13 EntryMessage (org.apache.logging.log4j.message.EntryMessage)9 StringFormattedMessage (org.apache.logging.log4j.message.StringFormattedMessage)8 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)8 Result (org.apache.logging.log4j.core.Filter.Result)7 LocalizedMessage (org.apache.logging.log4j.message.LocalizedMessage)6 ReusableMessage (org.apache.logging.log4j.message.ReusableMessage)6 IOException (java.io.IOException)5 ClockFactoryTest (org.apache.logging.log4j.core.util.ClockFactoryTest)5 ReusableObjectMessage (org.apache.logging.log4j.message.ReusableObjectMessage)5 MutableThreadContextStack (org.apache.logging.log4j.spi.MutableThreadContextStack)5