I am working in a sensitive data mask task where all the pattern have to come from Spring Cloud Config. Actually my problem is cannot access the property from the log4j2 class. It always null.
@Plugin(name="SensitiveDataLog", category = "Converter")
@ConverterKeys({"sense"})
public class SensitiveDataLog extends LogEventPatternConverter {
@Value("${ssn}")
private String ssn; //Always Null
private final String SSN_REGEX = "([0-9]{9})"; // We can keep patterns like this But i want these patterns comes from property.
private final Pattern SSN_PATTERN = Pattern.compile(SSN_REGEX);
public SensitiveDataLog(String name, String style) {
super(name, style);
}
public static SensitiveDataLog newInstance(String[] options) {
return new SensitiveDataLog("sense","sense");
}
@Override
public void format(LogEvent logEvent, StringBuilder outputMsg) {
String message = logEvent.getMessage().getFormattedMessage();
Matcher matcher = SSN_PATTERN.matcher(message);
if (matcher.find()) {
String maskedMessage = matcher.replaceAll("***-**-****");
outputMsg.append(maskedMessage);
} else {
outputMsg.append(message);
}
}
}
Suppose I am using @Value from another class ssn value will be invoked.
Any idea on this?
question from:
https://stackoverflow.com/questions/66060805/how-to-use-value-in-the-log4j2-abstractpatternconverter-spring-boot 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…