I have written a custom jar to be called from log4j that will mask sensitive data before being written log log. The class works as expected but now my ajp threads have increased 5x. In log4j I just changed: -layout class="org.apache.log4j.PatternLayout" to: -layout class="com.custom.protectdata.DataFilteringLayout"
Below is main class, question is should I be doing some kind of object cleanup, set asynchronous, how can I improve this code that will not increase my threads? This is running on a jboss server set in DEBUG logging and high volume traffic. I didn't notice a large increase in threads on my test box, but on production box with high traffic the threads increased 5x
DataFilteringLayout.java
public class DataFilteringLayout extends PatternLayout {
@Override
public String format(LoggingEvent event)
{
if (event.getMessage() instanceof String) {
String maskedMessage = event.getRenderedMessage();
boolean changedMessage = false;
// IDENTIFY SENSITIVE DATA FROM PROPERTY FILE KEYS
GetProtectedDataProp protectData = new GetProtectedDataProp();
Set<Object> keys = protectData.getAllKeys();
for (Object k : keys) {
String key = (String) k;
// RETRIEVE REGEX PATTERN FROM PROPERTY FILE
Pattern pkey = Pattern.compile(protectData.getPropertyValue(key),Pattern.CASE_INSENSITIVE|Pattern.UNICODE_CASE);
// CHECK PATTERN AGAINST INCOMING MESSAGE
Matcher matchPattern = pkey.matcher(maskedMessage);
// IF PATTERN FOUND MODIFY MESSAGE WITH MASKED FORMAT
if (matchPattern.find()) {
// IDENTIFY OUTPUT FORMAT OF MASKED DATA
GetMaskedFormatProp maskFormat = new GetMaskedFormatProp();
// APPLY REGEX PATTERN AGAINST RECEIVED MESSAGE
maskedMessage = matchPattern.replaceAll(maskFormat.getPropertyValue(key));
changedMessage = true;
}
}
if (changedMessage){
String maskedEvent = logMaskedData(maskedMessage, event);
return maskedEvent;
}
}
return super.format(event);
}
// CREATE A NEW LOGGING EVENT WITH THE MODIFIED (masked) STRING
public String logMaskedData(String maskedMessage, LoggingEvent event)
{
Throwable throwable = event.getThrowableInformation() != null ? event.getThrowableInformation().getThrowable() : null;
LoggingEvent maskedEvent = new LoggingEvent(event.fqnOfCategoryClass, Logger.getLogger(event.getLoggerName()),
event.timeStamp, event.getLevel(), maskedMessage, throwable);
return super.format(maskedEvent);
}
Thanks in advance for any suggestions!