云日志服务 LTS-使用Flume采集器上报日志到LTS:使用默认拦截器处理日志

时间:2024-08-02 09:01:49

使用默认拦截器处理日志

使用Flume采集器时,拦截器是简单的插件式组件,设置在Source和Channel之间。Source接收到的事件Event,在写入Channel之前,拦截器都可以进行转换或者删除这些事件。每个拦截器只处理同一个Source接收到的事件。

  • 时间戳拦截器

    该拦截器的作用是将时间戳插入到flume的事件报头中。如果不使用任何拦截器,flume接受到的只有message。时间戳拦截器的配置, 参数默认值描述type,类型名称timestamp,也可以使用类名的全路径preserveExisting为false。如果设置为true,若事件中报头已经存在,不会替换时间戳报头的值。source连接到时间戳拦截器的配置:

    a1.sources.r1.interceptors = timestamp 
    a1.sources.r1.interceptors.timestamp.type=timestamp 
    a1.sources.r1.interceptors.timestamp.preserveExisting=false
  • 正则过滤拦截器

    在日志采集的时候,可能有一些数据是不需要的,添加过滤拦截器可以过滤掉不需要的日志,也可以根据需要收集满足正则条件的日志。参数默认值描述type,类型名称REGEX_FILTER。excludeEvents为false时默认收集匹配到的事件。如果为true,则会删除匹配到的event,收集未匹配到的。source连接到正则过滤拦截器的配置:

    a1.sources.r1.interceptors = regex 
    a1.sources.r1.interceptors.regex.type=REGEX_FILTER 
    a1.sources.r1.interceptors.regex.regex=(today)|(Monday) 
    a1.sources.r1.interceptors.regex.excludeEvents=false

    这样配置的拦截器就只会接收日志消息中带有today或者Monday的日志。

  • 搜索并替换拦截器

    拦截器基于Java正则表达式提供简单的基于字符串的搜索和替换功能。配置如下:

    # 拦截器别名
    a1.sources.r1.interceptors = search-replace
    # 拦截器类型,必须是search_replace
    a1.sources.r1.interceptors.search-replace.type = search_replace
    
    #删除事件正文中的字符,根据正则匹配event内容
    a1.sources.r1.interceptors.search-replace.searchPattern = today
    # 替换匹配到的event内容
    a1.sources.r1.interceptors.search-replace.replaceString = yesterday
    # 设置字符集,默认是utf8
    a1.sources.r1.interceptors.search-replace.charset = utf8
support.huaweicloud.com/bestpractice-lts/lts_04_1131.html