拿身份证、手机号为例
1.新增日志输出过滤类
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.spi.FilterReply;
import ch.qos.logback.core.filter.AbstractMatcherFilter;
public class SensitiveDataMaskingInterceptor extends AbstractMatcherFilter<ILoggingEvent> {
@Override
public FilterReply decide(ILoggingEvent event) {
if (event.getMessage() != null) {
String maskedMessage = maskSensitiveData(event.getMessage());
event.setMessage(maskedMessage);
}
return FilterReply.NEUTRAL;
}
private String maskSensitiveData(String message) {
// Replace phone numbers and ID numbers with asterisks
String maskedMessage = message
.replaceAll("\\b\\d{11}\\b", "***********") // Phone number
.replaceAll("\\b\\d{17}\\w\\b", "*****************"); // ID number
return maskedMessage;
}
}
2.配置logback.xml文件
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<filter class="com.example.SensitiveDataMaskingInterceptor" />
<encoder>
<pattern>%d [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
</configuration>
其他的信息做混淆输出处理,同理,需要添加进行替换的表达式