背景
继上次log4j2做完日志掩码处理后, 发现其他项目中有用到logback, 于是同样去官网翻了下, 发现基本语法差不多, 不过语法稍微有些差异~
replace(p){r, t} | Replaces occurrences of 'r', a regex, with its replacement 't' in the string produces by the sub-pattern 'p'. For example, "%replace(%msg){'\s', ''}" will remove all spaces contained in the event message. The pattern 'p' can be arbitrarily complex and in particular can contain multiple conversion keywords. For instance, "%replace(%logger %msg){'\.', '/'}" will replace all dots in the logger or the message of the event with a forward slash. |
有个特殊的地方, 就是因为具体的替换规则是写在value里的, 双引号需转义
具体操作贴在下面
<Property name="REGEX" value="%replace(%msg){'"([1-9][0-9]{2})([0-9]{6})([0-9]+|[0-9]+X)', '"$1***$3'}"/>
<property name="ENCODER_PATTERN"
value="%d{yyyy-MM-dd HH:mm:ss.SSS} [${APP_NAME}] [%thread] %-5level [%logger] %M-${REGEX}%n"/>
结果
logger.info("\"123091023012930129031");
logger.info("1230910230129\"301290313132");
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"123***012930129031
1230910230129"301***132
其他方法自行研究吧~
官网地址: Chapter 6: Layouts