Search in sources :

Example 6 with ReadOnlyStringMap

use of org.apache.logging.log4j.util.ReadOnlyStringMap in project logging-log4j2 by apache.

the class ContextDataJsonAttributeConverterTest method testConvert02.

@Test
public void testConvert02() {
    final StringMap map = new SortedArrayStringMap();
    map.putValue("someKey", "coolValue");
    map.putValue("anotherKey", "testValue");
    map.putValue("myKey", "yourValue");
    final String converted = this.converter.convertToDatabaseColumn(map);
    assertNotNull("The converted value should not be null.", converted);
    final ReadOnlyStringMap reversed = this.converter.convertToEntityAttribute(converted);
    assertNotNull("The reversed value should not be null.", reversed);
    assertEquals("The reversed value is not correct.", map, reversed);
}
Also used : SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) StringMap(org.apache.logging.log4j.util.StringMap) ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap) ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) Test(org.junit.Test)

Example 7 with ReadOnlyStringMap

use of org.apache.logging.log4j.util.ReadOnlyStringMap in project logging-log4j2 by apache.

the class Log4j1XmlLayout method formatTo.

private void formatTo(final LogEvent event, final StringBuilder buf) {
    // We yield to the \r\n heresy.
    buf.append("<log4j:event logger=\"");
    buf.append(Transform.escapeHtmlTags(event.getLoggerName()));
    buf.append("\" timestamp=\"");
    buf.append(event.getTimeMillis());
    buf.append("\" level=\"");
    buf.append(Transform.escapeHtmlTags(String.valueOf(event.getLevel())));
    buf.append("\" thread=\"");
    buf.append(Transform.escapeHtmlTags(event.getThreadName()));
    buf.append("\">\r\n");
    buf.append("<log4j:message><![CDATA[");
    // Append the rendered message. Also make sure to escape any existing CDATA sections.
    Transform.appendEscapingCData(buf, event.getMessage().getFormattedMessage());
    buf.append("]]></log4j:message>\r\n");
    final List<String> ndc = event.getContextStack().asList();
    if (!ndc.isEmpty()) {
        buf.append("<log4j:NDC><![CDATA[");
        Transform.appendEscapingCData(buf, Strings.join(ndc, ' '));
        buf.append("]]></log4j:NDC>\r\n");
    }
    @SuppressWarnings("ThrowableResultOfMethodCallIgnored") final Throwable thrown = event.getThrown();
    if (thrown != null) {
        buf.append("<log4j:throwable><![CDATA[");
        buf.append(thrown.toString());
        buf.append("\r\n");
        for (final StackTraceElement element : thrown.getStackTrace()) {
            Transform.appendEscapingCData(buf, "\tat " + element.toString());
            buf.append("\r\n");
        }
        buf.append("]]></log4j:throwable>\r\n");
    }
    if (locationInfo) {
        final StackTraceElement source = event.getSource();
        if (source != null) {
            buf.append("<log4j:locationInfo class=\"");
            buf.append(Transform.escapeHtmlTags(source.getClassName()));
            buf.append("\" method=\"");
            buf.append(Transform.escapeHtmlTags(source.getMethodName()));
            buf.append("\" file=\"");
            buf.append(Transform.escapeHtmlTags(source.getFileName()));
            buf.append("\" line=\"");
            buf.append(source.getLineNumber());
            buf.append("\"/>\r\n");
        }
    }
    if (properties) {
        final ReadOnlyStringMap contextMap = event.getContextData();
        if (!contextMap.isEmpty()) {
            buf.append("<log4j:properties>\r\n");
            contextMap.forEach(new BiConsumer<String, String>() {

                @Override
                public void accept(final String key, final String val) {
                    if (val != null) {
                        buf.append("<log4j:data name=\"");
                        buf.append(Transform.escapeHtmlTags(key));
                        buf.append("\" value=\"");
                        buf.append(Transform.escapeHtmlTags(val));
                        buf.append("\"/>\r\n");
                    }
                }
            });
            buf.append("</log4j:properties>\r\n");
        }
    }
    buf.append("</log4j:event>\r\n\r\n");
}
Also used : ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap)

Aggregations

ReadOnlyStringMap (org.apache.logging.log4j.util.ReadOnlyStringMap)7 StringMap (org.apache.logging.log4j.util.StringMap)3 SortedArrayStringMap (org.apache.logging.log4j.util.SortedArrayStringMap)2 Test (org.junit.Test)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 PersistenceException (javax.persistence.PersistenceException)1 Marker (org.apache.logging.log4j.Marker)1 ThreadContext (org.apache.logging.log4j.ThreadContext)1 AppenderLoggingException (org.apache.logging.log4j.core.appender.AppenderLoggingException)1 IndexedReadOnlyStringMap (org.apache.logging.log4j.util.IndexedReadOnlyStringMap)1