Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

logging - How to use @Value in the log4j2 AbstractPatternConverter Spring Boot?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...