Commit ebfda018 authored by Java-刘 彧阳's avatar Java-刘 彧阳

测试日志脱敏

parent 3f3bbde4
...@@ -4,7 +4,11 @@ import ch.qos.logback.classic.Level; ...@@ -4,7 +4,11 @@ import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy; import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.classic.spi.LoggerContextVO; import ch.qos.logback.classic.spi.LoggerContextVO;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Marker; import org.slf4j.Marker;
/** /**
...@@ -12,6 +16,8 @@ import org.slf4j.Marker; ...@@ -12,6 +16,8 @@ import org.slf4j.Marker;
*/ */
public class WithOutPhoneLoggingEvent implements ILoggingEvent { public class WithOutPhoneLoggingEvent implements ILoggingEvent {
private static Pattern pattern = Pattern.compile("(?<!\\d)(?:(?:1[34578]\\d{9})|(?:861[34578]\\d{9}))(?!\\d)");
private ILoggingEvent event; private ILoggingEvent event;
transient String withOutPhoneFormattedMessage; transient String withOutPhoneFormattedMessage;
...@@ -41,7 +47,8 @@ public class WithOutPhoneLoggingEvent implements ILoggingEvent { ...@@ -41,7 +47,8 @@ public class WithOutPhoneLoggingEvent implements ILoggingEvent {
return withOutPhoneFormattedMessage; return withOutPhoneFormattedMessage;
} }
//todo 用正则找到手机号,并打上掩码,替换进去 //todo 用正则找到手机号,并打上掩码,替换进去
withOutPhoneFormattedMessage = event.getFormattedMessage().concat("##test##"); //withOutPhoneFormattedMessage = event.getFormattedMessage();
withOutPhoneFormattedMessage = coverPhone(event.getFormattedMessage());
return withOutPhoneFormattedMessage; return withOutPhoneFormattedMessage;
} }
...@@ -87,4 +94,21 @@ public class WithOutPhoneLoggingEvent implements ILoggingEvent { ...@@ -87,4 +94,21 @@ public class WithOutPhoneLoggingEvent implements ILoggingEvent {
// fixes http://jira.qos.ch/browse/LBCLASSIC-104 // fixes http://jira.qos.ch/browse/LBCLASSIC-104
this.getMDCPropertyMap(); this.getMDCPropertyMap();
} }
private static String coverPhone(String str){
if(str.length()<=0)
return "";
Matcher matcher = pattern.matcher(str);
Map<String,String> replaceMap = new HashMap<>();
while (matcher.find()) {
String group = matcher.group();
String replace = group.substring(0,3).concat("****").concat(group.substring(7));
replaceMap.put(group,replace);
}
Set<Map.Entry<String, String>> entries = replaceMap.entrySet();
for (Map.Entry<String, String> entry :entries){
str = str.replaceAll(entry.getKey(),entry.getValue());
}
return str;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment