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