Created
February 9, 2017 17:23
-
-
Save rahulaga/8bd7ca7b521a7858e86c0a51fffe3f03 to your computer and use it in GitHub Desktop.
Logback masking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Set; | |
import java.util.regex.Pattern; | |
import org.apache.commons.lang3.StringUtils; | |
import ch.qos.logback.classic.pattern.ClassicConverter; | |
import ch.qos.logback.classic.spi.ILoggingEvent; | |
/** | |
* Masking configured values in logs | |
* | |
* @author Rahul Agarwal | |
* | |
*/ | |
public class MaskingConverter extends ClassicConverter { | |
private Map<Pattern, String> patternMap = new HashMap<>(); | |
public static final String MASK = "*****"; | |
@Override | |
public String convert(ILoggingEvent logEvent) { | |
String message = logEvent.getMessage(); | |
Set<Pattern> patternSet = patternMap.keySet(); | |
if (StringUtils.isNotBlank(message)) { | |
for (Pattern pattern : patternSet) { | |
message = pattern.matcher(message).replaceAll(patternMap.get(pattern)); | |
} | |
} | |
return message; | |
} | |
@Override | |
public void start() { | |
List<String> options = getOptionList(); | |
if (options != null && options.size() > 0) { | |
// 0 = CompleteMask | |
patternMap.put(Pattern.compile("(?x)([\"]?(" + options.get(0) + ")[\"]?\\s*[:=]{1}\\s*[\"]?)(?:[^\"\\n]+)"), "$1" + MASK); | |
// 1 = MaskExceptFour | |
patternMap.put(Pattern.compile("(?x)([\"]?(" + options.get(1) + ")[\"]?[:=]{1}[\"]?[\\w.+/=]+)(?:[\\w.+/=]{4})"), "$1" + MASK); | |
// 3 = MaskAllButFour | |
patternMap.put(Pattern.compile("(?x)([\"]?(" + options.get(2) + ")[\"]?[:=]{1}[\"]?)(?:[\\w.+/=]+(?=\\w{4}))"), "$1" + MASK); | |
// 4 = emailMasking | |
patternMap.put(Pattern.compile("(?x)([\"]?(" + options.get(3) + ")[\"]?\\s*[:=]{1}\\s*[\"]?[\\w.]+(?=@[\\w.]+))(?:@[\\w.]+)"), "$1" + MASK); | |
} | |
super.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment