1. 程式人生 > >logback身份證脫敏

logback身份證脫敏

ride get class ring ide details shift || nbsp

logback身份證脫敏

學習了:https://shift-alt-ctrl.iteye.com/blog/2425469

https://blog.csdn.net/fywfengyanwei/article/details/78484590

java:

package com.util;
import ch.qos.logback.classic.pattern.MessageConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ComplexMessageConverter extends MessageConverter { @Override public String convert(ILoggingEvent event) { String source = event.getFormattedMessage(); if (source == null || source.isEmpty()) { return source; } // 身份證號碼脫敏 String card = "([1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx])|" + "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3})"; Pattern p
= Pattern.compile(card); Matcher matcher = p.matcher(source); source = matcher.replaceAll("idCardNo"); return source; } }

logback.xml:

<conversionRule conversionWord="m"
                converterClass="com.util.ComplexMessageConverter"></conversionRule>

logback身份證脫敏