use of org.apache.logging.log4j.util.SortedArrayStringMap in project logging-log4j2 by apache.
the class MapRewritePolicy method rewrite.
/**
* {@inheritDoc}
*/
public LoggingEvent rewrite(final LoggingEvent source) {
Object msg = source.getMessage();
if (msg instanceof MapMessage || msg instanceof Map) {
Map<String, String> props = source.getProperties() != null ? new HashMap<>(source.getProperties()) : new HashMap<>();
@SuppressWarnings("unchecked") Map<String, Object> eventProps = msg instanceof Map ? (Map) msg : ((MapMessage) msg).getData();
//
// if the map sent in the logging request
// has "message" entry, use that as the message body
// otherwise, use the entire map.
//
Message newMessage = null;
Object newMsg = eventProps.get("message");
if (newMsg != null) {
newMessage = new SimpleMessage(newMsg.toString());
for (Map.Entry<String, Object> entry : eventProps.entrySet()) {
if (!("message".equals(entry.getKey()))) {
props.put(entry.getKey(), entry.getValue().toString());
}
}
} else {
return source;
}
LogEvent event;
if (source instanceof LogEventAdapter) {
event = new Log4jLogEvent.Builder(((LogEventAdapter) source).getEvent()).setMessage(newMessage).setContextData(new SortedArrayStringMap(props)).build();
} else {
LocationInfo info = source.getLocationInformation();
StackTraceElement element = new StackTraceElement(info.getClassName(), info.getMethodName(), info.getFileName(), Integer.parseInt(info.getLineNumber()));
Thread thread = getThread(source.getThreadName());
long threadId = thread != null ? thread.getId() : 0;
int threadPriority = thread != null ? thread.getPriority() : 0;
event = Log4jLogEvent.newBuilder().setContextData(new SortedArrayStringMap(props)).setLevel(OptionConverter.convertLevel(source.getLevel())).setLoggerFqcn(source.getFQNOfLoggerClass()).setMarker(null).setMessage(newMessage).setSource(element).setLoggerName(source.getLoggerName()).setThreadName(source.getThreadName()).setThreadId(threadId).setThreadPriority(threadPriority).setThrown(source.getThrowableInformation().getThrowable()).setTimeMillis(source.getTimeStamp()).setNanoTime(0).setThrownProxy(null).build();
}
return new LogEventAdapter(event);
} else {
return source;
}
}
use of org.apache.logging.log4j.util.SortedArrayStringMap in project logging-log4j2 by apache.
the class CaseConverterResolverTest method test_upper.
@ParameterizedTest
@CsvSource({ // case | locale | input | output
"upper" + ",nl" + ",ioz" + ",IOZ", "upper" + ",nl" + ",IOZ" + ",IOZ", "lower" + ",nl" + ",ioz" + ",ioz", "lower" + ",nl" + ",IOZ" + ",ioz", "upper" + ",tr" + ",ıiğüşöç" + ",IİĞÜŞÖÇ", "upper" + ",tr" + ",IİĞÜŞÖÇ" + ",IİĞÜŞÖÇ", "lower" + ",tr" + ",ıiğüşöç" + ",ıiğüşöç", "lower" + ",tr" + ",IİĞÜŞÖÇ" + ",ıiğüşöç" })
void test_upper(final String case_, final String locale, final String input, final String output) {
// Create the event template.
final String eventTemplate = writeJson(asMap("output", asMap("$resolver", "caseConverter", "case", case_, "locale", locale, "input", asMap("$resolver", "mdc", "key", "input"))));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
// Create the log event.
final StringMap contextData = new SortedArrayStringMap();
contextData.putValue("input", input);
final LogEvent logEvent = Log4jLogEvent.newBuilder().setContextData(contextData).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> assertThat(accessor.getString("output")).isEqualTo(output));
}
use of org.apache.logging.log4j.util.SortedArrayStringMap in project logging-log4j2 by apache.
the class ReadOnlyStringMapResolverTest method test_mdc_key_access.
@Test
void test_mdc_key_access() {
// Create the log event.
final SimpleMessage message = new SimpleMessage("Hello, World!");
final StringMap contextData = new SortedArrayStringMap();
final String mdcDirectlyAccessedKey = "mdcKey1";
final String mdcDirectlyAccessedValue = "mdcValue1";
contextData.putValue(mdcDirectlyAccessedKey, mdcDirectlyAccessedValue);
final String mdcDirectlyAccessedNullPropertyKey = "mdcKey2";
contextData.putValue(mdcDirectlyAccessedNullPropertyKey, null);
final LogEvent logEvent = Log4jLogEvent.newBuilder().setMessage(message).setContextData(contextData).build();
// Check the serialized event.
testReadOnlyStringMapKeyAccess(mdcDirectlyAccessedKey, mdcDirectlyAccessedValue, mdcDirectlyAccessedNullPropertyKey, logEvent, "mdc");
}
use of org.apache.logging.log4j.util.SortedArrayStringMap in project logging-log4j2 by apache.
the class ReadOnlyStringMapResolverTest method pattern_replacement_should_work.
@Test
void pattern_replacement_should_work() {
// Create the event template.
final String eventTemplate = writeJson(asMap("$resolver", "mdc", "pattern", "user:(role|rank)", "replacement", "$1"));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
// Create the log event.
final StringMap contextData = new SortedArrayStringMap();
contextData.putValue("user:role", "engineer");
contextData.putValue("user:rank", "senior");
final LogEvent logEvent = Log4jLogEvent.newBuilder().setContextData(contextData).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
assertThat(accessor.getString("role")).isEqualTo("engineer");
assertThat(accessor.getString("rank")).isEqualTo("senior");
});
}
use of org.apache.logging.log4j.util.SortedArrayStringMap in project logging-log4j2 by apache.
the class JsonWriterTest method test_writeObject_IndexedReadOnlyStringMap.
@Test
void test_writeObject_IndexedReadOnlyStringMap() {
final IndexedReadOnlyStringMap map = new SortedArrayStringMap(new LinkedHashMap<String, Object>() {
{
put("buzz", 1.2D);
put("foo", "bar");
}
});
final String expectedJson = "{'buzz':1.2,'foo':'bar'}".replace('\'', '"');
final String actualJson = withLockedWriterReturning(writer -> writer.use(() -> writer.writeObject(map)));
Assertions.assertThat(actualJson).isEqualTo(expectedJson);
}
Aggregations