use of org.apache.logging.log4j.core.util.KeyValuePair in project logging-log4j2 by apache.
the class Rfc5424LayoutTest method testDiscardEmptyLoggerFields.
@Test
public void testDiscardEmptyLoggerFields() {
final String mdcId = "RequestContext";
Arrays.asList("[BAZ@32473 baz=\"org.apache.logging.log4j.core.layout.Rfc5424LayoutTest.testLoggerFields\"]" + "[RequestContext@3692 bar=\"org.apache.logging.log4j.core.layout.Rfc5424LayoutTest.testLoggerFields\"]");
for (final Appender appender : root.getAppenders().values()) {
root.removeAppender(appender);
}
final LoggerFields[] loggerFields = new LoggerFields[] { LoggerFields.createLoggerFields(new KeyValuePair[] { new KeyValuePair("dummy", Strings.EMPTY), new KeyValuePair("empty", Strings.EMPTY) }, "SD-ID", "32473", true), LoggerFields.createLoggerFields(new KeyValuePair[] { new KeyValuePair("baz", "%C.%M"), new KeyValuePair("baz", "%C.%M") }, "BAZ", "32473", false), LoggerFields.createLoggerFields(new KeyValuePair[] { new KeyValuePair("bar", "%C.%M") }, null, null, false) };
final AbstractStringLayout layout = Rfc5424Layout.createLayout(Facility.LOCAL0, "Event", 3692, true, mdcId, null, null, true, null, "ATM", null, "key1, key2, locale", null, null, null, false, loggerFields, null);
final ListAppender appender = new ListAppender("List", null, layout, true, false);
appender.start();
root.addAppender(appender);
root.setLevel(Level.DEBUG);
root.info("starting logger fields test");
try {
final List<String> list = appender.getMessages();
assertTrue("Not enough list entries", list.size() > 0);
final String message = list.get(0);
Assert.assertTrue("SD-ID should have been discarded", !message.contains("SD-ID"));
Assert.assertTrue("BAZ should have been included", message.contains("BAZ"));
Assert.assertTrue(mdcId + "should have been included", message.contains(mdcId));
appender.clear();
} finally {
root.removeAppender(appender);
appender.stop();
}
}
use of org.apache.logging.log4j.core.util.KeyValuePair in project logging-log4j2 by apache.
the class ThreadContextMapFilter method createFilter.
@PluginFactory
public static ThreadContextMapFilter createFilter(@PluginElement("Pairs") final KeyValuePair[] pairs, @PluginAttribute("operator") final String oper, @PluginAttribute("onMatch") final Result match, @PluginAttribute("onMismatch") final Result mismatch) {
if (pairs == null || pairs.length == 0) {
LOGGER.error("key and value pairs must be specified for the ThreadContextMapFilter");
return null;
}
final Map<String, List<String>> map = new HashMap<>();
for (final KeyValuePair pair : pairs) {
final String key = pair.getKey();
if (key == null) {
LOGGER.error("A null key is not valid in MapFilter");
continue;
}
final String value = pair.getValue();
if (value == null) {
LOGGER.error("A null value for key " + key + " is not allowed in MapFilter");
continue;
}
List<String> list = map.get(pair.getKey());
if (list != null) {
list.add(value);
} else {
list = new ArrayList<>();
list.add(value);
map.put(pair.getKey(), list);
}
}
if (map.isEmpty()) {
LOGGER.error("ThreadContextMapFilter is not configured with any valid key value pairs");
return null;
}
final boolean isAnd = oper == null || !oper.equalsIgnoreCase("or");
return new ThreadContextMapFilter(map, isAnd, match, mismatch);
}
use of org.apache.logging.log4j.core.util.KeyValuePair 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;
}
use of org.apache.logging.log4j.core.util.KeyValuePair in project logging-log4j2 by apache.
the class StructuredDataFilter method createFilter.
/**
* Create the StructuredDataFilter.
* @param pairs Key and value pairs.
* @param oper The operator to perform. If not "or" the operation will be an "and".
* @param match The action to perform on a match.
* @param mismatch The action to perform on a mismatch.
* @return The StructuredDataFilter.
*/
@PluginFactory
public static StructuredDataFilter createFilter(@PluginElement("Pairs") final KeyValuePair[] pairs, @PluginAttribute("operator") final String oper, @PluginAttribute("onMatch") final Result match, @PluginAttribute("onMismatch") final Result mismatch) {
if (pairs == null || pairs.length == 0) {
LOGGER.error("keys and values must be specified for the StructuredDataFilter");
return null;
}
final Map<String, List<String>> map = new HashMap<>();
for (final KeyValuePair pair : pairs) {
final String key = pair.getKey();
if (key == null) {
LOGGER.error("A null key is not valid in MapFilter");
continue;
}
final String value = pair.getValue();
if (value == null) {
LOGGER.error("A null value for key " + key + " is not allowed in MapFilter");
continue;
}
List<String> list = map.get(pair.getKey());
if (list != null) {
list.add(value);
} else {
list = new ArrayList<>();
list.add(value);
map.put(pair.getKey(), list);
}
}
if (map.isEmpty()) {
LOGGER.error("StructuredDataFilter is not configured with any valid key value pairs");
return null;
}
final boolean isAnd = oper == null || !oper.equalsIgnoreCase("or");
return new StructuredDataFilter(map, isAnd, match, mismatch);
}
use of org.apache.logging.log4j.core.util.KeyValuePair in project logging-log4j2 by apache.
the class StructuredDataFilterTest method testFilter.
@Test
public void testFilter() {
final KeyValuePair[] pairs = new KeyValuePair[] { new KeyValuePair("id.name", "AccountTransfer"), new KeyValuePair("ToAccount", "123456") };
StructuredDataFilter filter = StructuredDataFilter.createFilter(pairs, "and", null, null);
filter.start();
StructuredDataMessage msg = new StructuredDataMessage("AccountTransfer@18060", "Transfer Successful", "Audit");
msg.put("ToAccount", "123456");
msg.put("FromAccount", "211000");
msg.put("Amount", "1000.00");
assertTrue(filter.isStarted());
assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.DEBUG, null, msg, null));
msg.put("ToAccount", "111111");
assertSame(Filter.Result.DENY, filter.filter(null, Level.ERROR, null, msg, null));
filter = StructuredDataFilter.createFilter(pairs, "or", null, null);
filter.start();
msg = new StructuredDataMessage("AccountTransfer@18060", "Transfer Successful", "Audit");
msg.put("ToAccount", "123456");
msg.put("FromAccount", "211000");
msg.put("Amount", "1000.00");
assertTrue(filter.isStarted());
assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.DEBUG, null, msg, null));
msg.put("ToAccount", "111111");
assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.ERROR, null, msg, null));
}
Aggregations