Skip to content

Instantly share code, notes, and snippets.

@nattaphonjeamjit
Last active April 9, 2018 17:14
Show Gist options
  • Save nattaphonjeamjit/34afd577bf03627f0d9d0aebeb42f529 to your computer and use it in GitHub Desktop.
Save nattaphonjeamjit/34afd577bf03627f0d9d0aebeb42f529 to your computer and use it in GitHub Desktop.
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Plugin(name = "EraseSensitiveData", category = "Converter")
@ConverterKeys({ "msg" })
public class EraseSensitiveDataConverter extends LogEventPatternConverter {
private static final String NAME = "msg";
private static final String JSON_REPLACEMENT_REGEX_1 = "\"$1\":\"٩(◕‿◕。)۶\"";
private static final String JSON_REPLACEMENT_REGEX_2 = "\"$1\":\"(づ ◕‿◕ )づ\"";
private static final String JSON_KEYS = "sensitive1|sensitive2"
private static final Pattern JSON_PATTERN = Pattern.compile(/"(${JSON_KEYS})": "([^"]+)"/)
public EraseSensitiveDataConverter(String[] options) {
super(NAME, NAME); // What !?
}
public static EraseSensitiveDataConverter newInstance(final String[] options) {
return new EraseSensitiveDataConverter(options);
}
@Override
public void format(LogEvent logEvent, StringBuilder outputMsg) {
Message message = logEvent.getMessage();
Message outMessage = new ParameterizedMessage(message.getFormattedMessage(),
message.getParameters(),
message.getThrowable());
outputMsg.append(this.converter(outMessage.getFormat()));
}
private String converter(String message) {
StringBuffer buffer = new StringBuffer();
Matcher matcher = JSON_PATTERN.matcher(message);
int i = 1;
while (matcher.find()) {
matcher.appendReplacement(buffer, i%2==0 ? JSON_REPLACEMENT_REGEX_1 : JSON_REPLACEMENT_REGEX_2);
i++;
}
matcher.appendTail(buffer);
return buffer.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment