dasun_001
dasun_001

Reputation: 169

How to load application.properties into Log4j2 LogEventPatternConverter class?

I am working on a task that I want to mask sensitive data using Log4j2 LogEventPatternConverter Class.

@Plugin(name="SensitiveDataLog", category = "Converter")
@ConverterKeys({"sense"})
public class SensitiveDataLog extends LogEventPatternConverter {

    @Value("${ssn}")
    private String ssn;

    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 want to keep pattern in application.properties, But problem here is we cannot load property value ssn. Always its null.

Here is my log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info" monitorInterval="30"
               packages="com.virtusa.xlab.fw.logging.component"
               xmlns="http://logging.apache.org/log4j/2.0/config">
    <Properties>
        <Property name="basePath">logs/log4j2</Property>
    </Properties>
    <Appenders>
        <!-- File Appender -->
        <RollingFile name="FILE"
                     fileName="${basePath}/logfile.log" filePattern="${basePath}/logfile.%d{yyyy-MM-dd}-%i.log" append="true">
            <PatternLayout
                    pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %sense%n" />
            <Policies>
                <SizeBasedTriggeringPolicy size="1 KB" />
            </Policies>
            <DefaultRolloverStrategy max="4" />
        </RollingFile>
        <!-- Console Appender -->
        <Console name="STDOUT" target="SYSTEM_OUT">

            <PatternLayout
                    pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %sense%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="com.virtusa.xlab.fw" level="info" />
        <Root level="info">
            <AppenderRef ref="STDOUT" />
            <AppenderRef ref="FILE" />
        </Root>
    </Loggers>
</Configuration>

Can anyone help me out here?

Thanks.

Upvotes: 1

Views: 654

Answers (1)

LeoBarykin
LeoBarykin

Reputation: 36

The problem is that SensitiveDataLog is created via static method newInstance(). Obviously, field ssn is not initialized at that moment. What you can do is to init the field later, e.g. when refreshing Spring context. Here is my snippet:

private static XmlMaskPatternConverter INSTANCE = new XmlMaskPatternConverter();

public XmlMaskPatternConverter() {
    super(NAME, NAME);
}

public static XmlMaskPatternConverter newInstance() {
    return INSTANCE;
}

Now you can call static method getInstance() somewhere in your Spring Configuration (I do it in @Bean method) and set the ssn value there. Ofc, you need to create a setter for this field.

P.S. Hope it helps. I faced this problem too, so decided to leave my solution here. My first post on SO btw)

Upvotes: 2

Related Questions