You need to enable JavaScript to run this app.
导航

字符串替换插件

最近更新时间2023.11.13 16:31:40

首次发布时间2023.08.31 15:00:33

您可以在使用 LogCollector 采集日志时,使用 replace 插件对指定字段进行字符串替换,支持正则匹配或内容匹配方式。

说明

  • LogCollector V1.0.34 及后续版本支持该插件。如何查看 LogCollector 版本,请参考查看软件版本
  • replace 插件还可以用于数据脱敏场景,将指定字段中的敏感字符替换为其他字符串,例如将手机号码中间四位替换为 ****。该场景下建议关闭上传解析失败日志上传原始日志,以保护您的敏感信息。

参数说明

名称

类型

是否必选

说明

field

String

待替换的字段名称。
字段名称不可重复,不可为空,且不支持英文句号(.)。

type

String

日志内容的替换方式。支持设置为:

  • string:字符串替换。
  • regex:正则表达式替换。

pattern

String

日志内容的匹配模式。当原始字段中有一处或多处匹配 pattern 时,它们将全部被 replacement 对应的值替换。

  • 当 type 为 string 时,pattern 应指定为表示日志内容的字符串。
  • 当 type 为 regex 时,pattern 应指定为匹配日志内容的正则表达式。支持正则表达式捕获组。

replacement

String

用于替换指定日志内容的字符串。
如果希望支持正则表达式捕获组,replacement 必须包含 $n 形式的占位符,用来引用所捕获的内容。其中 n 表示该正则表达式捕获组的编号,从 1 开始从左往右计数。

when

Object

插件的执行条件,仅当执行条件判断为 true 时,才执行此插件。详细说明请参考插件执行条件

ignore_missing

Boolean

是否忽略不存在的字段。

  • true(默认值):当字段不存在,直接忽略。
  • false:当字段不存在时,直接报错。

配置示例

  • 经过采集模式或者其他处理器插件处理之后的键值对日志:

    "data": "Click Here"
    "size": "36"
    "style": "bold"
    "name": "text1"
    "hOffset": "250"
    "vOffset": "100"
    "alignment": "center"
    "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
    "time": "Today is 2023-06-08. Tomorrow is 2023-06-09."
    
  • LogCollector 插件处理器配置:

    {
        "processors":[
            {
                "replace":[
                    {
                        "field":"alignment",
                        "type":"string",
                        "pattern":"en",
                        "replacement":"xx"
                    },
                    {
                        "field":"data",
                        "type":"regex",
                        "pattern":"[a-h]+",
                        "replacement":"xx"
                    },
                    {
                        "field":"time",
                        "type":"regex",
                        "pattern":"(\\d{4})-(\\d{2})-(\\d{2})",
                        "replacement":"$2/$3/$1"
                    }
                ]
            }
        ]
    }
    
  • 处理结果:

    "data": "Clixxk Hxxrxx"
    "size": "36"
    "style": "bold"
    "name": "text1"
    "hOffset": "250"
    "vOffset": "100"
    "alignment": "cxxter"
    "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
    "time": "Today is 06/08/2023. Tomorrow is 06/09/2023."