added custom provider for masking message in FileAppender (#4)

This commit is contained in:
malkoas 2019-04-17 14:54:11 +03:00 committed by GitHub
parent 4a91277b41
commit f4d6292e4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 138 additions and 36 deletions

View File

@ -1,16 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="com.rbkmoney.logback.mask.PatternMaskingLayout">
<maskPattern>\b\d{6}(\d{2,9})\d{4}\b</maskPattern>
<maskPattern>(\b\d{3}\b)</maskPattern>
<pattern>%-5p [%d{ISO8601,UTC}] [%thread] %c: %m%n%rootException</pattern>
</layout>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="stdout"/>
</root>
</configuration>

11
pom.xml
View File

@ -29,6 +29,17 @@
<version>1.2.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>5.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.rbkmoney.logback</groupId>
<artifactId>nop-rolling</artifactId>
<version>1.0.0</version>
</dependency>
<!-- Test libs -->
<dependency>
<groupId>junit</groupId>

View File

@ -0,0 +1,25 @@
package com.rbkmoney.logback.mask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
public class MaskingMessageWithPattern {
public static String maskMessage(String message, Pattern multilinePattern) {
if (multilinePattern == null) {
return message;
}
StringBuilder sb = new StringBuilder(message);
Matcher matcher = multilinePattern.matcher(sb);
while (matcher.find()) {
IntStream.rangeClosed(1, matcher.groupCount()).forEach(group -> {
if ((matcher.group(group) != null)) {
IntStream.range(matcher.start(group), matcher.end(group))
.forEach(i -> sb.setCharAt(i, '*'));
}
});
}
return sb.toString();
}
}

View File

@ -5,9 +5,8 @@ import ch.qos.logback.classic.spi.ILoggingEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
public class PatternMaskingLayout extends PatternLayout{
@ -24,23 +23,6 @@ public class PatternMaskingLayout extends PatternLayout{
@Override
public String doLayout(ILoggingEvent event) {
return super.doLayout(new MaskedEvent(event, maskMessage(event.getFormattedMessage())));
}
private String maskMessage(String message) {
if (multilinePattern == null) {
return message;
}
StringBuilder sb = new StringBuilder(message);
Matcher matcher = multilinePattern.matcher(sb);
while (matcher.find()) {
IntStream.rangeClosed(1, matcher.groupCount()).forEach(group -> {
if ((matcher.group(group) != null)) {
IntStream.range(matcher.start(group), matcher.end(group))
.forEach(i -> sb.setCharAt(i, '*'));
}
});
}
return sb.toString();
return super.doLayout(new MaskedEvent(event, MaskingMessageWithPattern.maskMessage(event.getFormattedMessage(), multilinePattern)));
}
}

View File

@ -0,0 +1,43 @@
package com.rbkmoney.logback.mask;
import ch.qos.logback.classic.spi.ILoggingEvent;
import com.fasterxml.jackson.core.JsonGenerator;
import net.logstash.logback.composite.AbstractFieldJsonProvider;
import net.logstash.logback.composite.FieldNamesAware;
import net.logstash.logback.composite.JsonWritingUtils;
import net.logstash.logback.fieldnames.LogstashFieldNames;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
public class PatternMaskingMessageJsonProvider extends AbstractFieldJsonProvider<ILoggingEvent> implements FieldNamesAware<LogstashFieldNames> {
private Pattern multilinePattern;
private List<String> maskPatterns = new ArrayList<>();
public static final String FIELD_MESSAGE = "message";
public void addMaskPattern(String maskPattern) {
maskPatterns.add(maskPattern);
multilinePattern = Pattern.compile(
String.join("|", maskPatterns),
Pattern.MULTILINE
);
}
public PatternMaskingMessageJsonProvider() {
setFieldName(FIELD_MESSAGE);
}
@Override
public void writeTo(JsonGenerator generator, ILoggingEvent event) throws IOException {
JsonWritingUtils.writeStringField(generator, getFieldName(), MaskingMessageWithPattern.maskMessage(event.getFormattedMessage(),multilinePattern));
}
@Override
public void setFieldNames(LogstashFieldNames fieldNames) {
setFieldName(fieldNames.getMessage());
}
}

View File

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_FILE}</file>
<rollingPolicy class="com.rbkmoney.log.appender.NoopRollingPolicy"/>
<triggeringPolicy class="com.rbkmoney.log.appender.RotationBasedTriggeringPolicy">
<checkCachePeriod>5000</checkCachePeriod>
</triggeringPolicy>
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<mdc/>
<threadName/>
<provider class="com.rbkmoney.logback.mask.PatternMaskingMessageJsonProvider">
<maskPattern>\b\d{6}(\d{2,9})\d{4}\b</maskPattern>
<maskPattern>(\b\d{3}\b)</maskPattern>
</provider>
<version/>
<loggerName/>
<context/>
<pattern>
<pattern>
{
"@timestamp": "%date{yyy-MM-dd'T'HH:mm:ss.SSSXXX, UTC}",
"@severity": "%level",
"application": "@artifactId@",
"application_version": "@version@"
}
</pattern>
</pattern>
<stackTrace>
<throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
<shortenedClassNameLength>20</shortenedClassNameLength>
<rootCauseFirst>true</rootCauseFirst>
</throwableConverter>
</stackTrace>
</providers>
</encoder>
</appender>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="com.rbkmoney.logback.mask.PatternMaskingLayout">
<maskPattern>\b\d{6}(\d{2,9})\d{4}\b</maskPattern>
<maskPattern>(\b\d{3}\b)</maskPattern>
<pattern>%-5p [%d{ISO8601,UTC}] [%thread] %c: %m%n%rootException</pattern>
</layout>
</encoder>
</appender>
<root level="ALL">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</configuration>

View File

@ -128,4 +128,5 @@ public class PatternMaskingLayoutTest {
}
};
}
}