I need to filter Log4j output to remove "sensitive" information like passwords from the log messages. The goal is to do something like this:
Replace:
05-Jan-2018 INFO [org.my.application] Username=Bob Password=myWeakPassword
With:
05-Jan-2018 INFO [org.my.application] Username=Bob Password=*********
This is fairly easy to do in Log4j V1, by extending the PatternLayout class:
public class CustomPatternLayout extends org.apache.log4j.PatternLayout {
@Override
public String format(LoggingEvent event) {
String temp = super.format(event);
return doFilteringStuff(temp);
}
}
However, in Log4j V2 the PatternLayout class was made "final" and the whole architecture was changed. There no longer seems to be a simple way to intercept/override the calls to the PatternLayout object. I looked at the Apache documentation but there's not much information.
I checked this question and this question but neither one has much help to offer.
I realize this is a very "general" question but does anyone know a straightforward way to do this in Log4j V2, or have any advice on this?