All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.uid2.shared.logback.MaskingPatternLayout Maven / Gradle / Ivy

package com.uid2.shared.logback;

import ch.qos.logback.classic.PatternLayout;
import ch.qos.logback.classic.spi.ILoggingEvent;

import java.util.Map;
import java.util.regex.Pattern;

public class MaskingPatternLayout extends PatternLayout {
    private static final Map MASKING_PATTERNS = Map.of(
            Pattern.compile("\\S+s3\\.amazonaws\\.com/\\S*X-Amz-Security-Token=\\S+"), "REDACTED - S3"
    );

    @Override
    public String doLayout(ILoggingEvent event) {
        return mask(super.doLayout(event));
    }

    private String mask(String message) {
        if (message == null) {
            return null;
        }

        String maskedMessage = message;
        for (Map.Entry entry : MASKING_PATTERNS.entrySet()) {
            Pattern pattern = entry.getKey();
            String mask = entry.getValue();
            maskedMessage = pattern.matcher(maskedMessage).replaceAll(mask);
        }
        return maskedMessage;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy