前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Fluentd 过滤插件:grep 用法详解

Fluentd 过滤插件:grep 用法详解

作者头像
Fluentd中文网
发布2021-02-05 16:43:47
2.1K0
发布2021-02-05 16:43:47
举报
文章被收录于专栏:Fluentd学习交流Fluentd学习交流

filter_grep 是一个常用的过滤日志内容的插件。

熟悉或者使用过 Linux 系统的小伙伴应该知道,Linux 中有三个处理文本内容的利器:grep、awk 和 sed。这其中,grep 算是最常用的文本查找命令了。而正则表达式也是每个软件开发人员工作中不可避免会用到的文本处理方法。

正因为如此,Fluentd 内置了 grep 过滤插件,方便我们针对日志事件的某些字段进行过滤操作。


【配置示例】

我们先看一段配置片段:

代码语言:javascript
复制
<filter foo.bar>
  @type grep

  <regexp>
    key message
    pattern /cool/
  </regexp>

  <regexp>
    key hostname
    pattern /^web\d+\.example\.com$/
  </regexp>

  <exclude>
    key message
    pattern /uncool/
  </exclude>
</filter>

这个例子会去匹配满足如下三个条件的日志事件:

  1. 日志事件的 message 字段的值包含 cool 文本
  2. 日志事件的 hostname 字段的值匹配 web<数字>.example.com 形式
  3. 日志事件的 message 字段的值不含 uncool 文本

因此,如下日志内容会被 Fluentd 保留:

代码语言:javascript
复制
{"message":"It's cool outside today", "hostname":"web001.example.com"}
{"message":"That's not cool", "hostname":"web1337.example.com"}

而,如下日志内容会被 Fluentd 丢弃:

代码语言:javascript
复制
{"message":"I am cool but you are uncool", "hostname":"db001.example.com"}
{"hostname":"web001.example.com"}
{"message":"It's cool outside today"}

注意看一下,上边这3条日志,第一条 hostname 的值不合法,第二条和第三条都只含有一个字段。


【插件参数】

<regexp> 指令

key:必需参数,指定需要过滤的字段名

代码语言:javascript
复制
<regexp>
  key price
  pattern /[1-9]\d*/
</regexp>

当有多个 <regexp> 存在时,grep 插件仅保留满足所有<regexp>条件的日志。

代码语言:javascript
复制
<regexp>
  key price
  pattern /[1-9]\d*/
</regexp>

<regexp>
  key item_name
  pattern /^book_/
</regexp>

比如:

这个配置片段会匹配 item_name 字段以 book_ 或 article 开头的日志,其余日志将被丢弃。

如果你使用的 pattern 中包含前置的斜杠(比如,文件路径),你需要对这个前置的斜杠进行转义。否则,匹配结果可能未如所愿。

代码语言:javascript
复制
<regexp>
  key filepath
  pattern \/spool/
</regexp>

也可以向下边这样进行转义:

regexpN

每个 regexpN 接受以空白分隔的两个参数,分别表示key、pattern。

代码语言:javascript
复制
regexp1 price [1-9]\d*
regexp2 item_name ^book_

<exclude> 指令

包含两个参数:key 和 pattern

pattern:必需参数,指定过滤使用的正则表达式

比如,下边这个配置用于丢弃 status_code 为 5xx 的日志:

所以,对于如下这段配置

任一 status_code 字段值为 5xx 或者 url 字段值以 .css 结尾的日志都会被丢弃。

excludeN

每个 excludeN 接受以空白分隔的两个参数,分别表示key、pattern。

代码语言:javascript
复制
exclude1 status_code ^5\d\d$
exclude2 url \.css$

<and> 指令

满足<and>中所有 pattern 的日志才会被保留或丢弃。

代码语言:javascript
复制
<and>
  <regexp>
    key price
    pattern /[1-9]\d*/
  </regexp>

  <regexp>
    key item_name
    pattern /^book_/
  </regexp>
</and>
代码语言:javascript
复制
<regexp>
  key price
  pattern /[1-9]\d*/
</regexp>

<regexp>
  key item_name
  pattern /^book_/
</regexp>
代码语言:javascript
复制
<and>
  <exclude>
    key container_name
    pattern /^app\d{2}/
  </exclude>

  <exclude>
    key log_level
    pattern /^(?:debug|trace)$/
  </exclude>
</and>

<or> 指令

满足<or>中任一 pattern 的日志就会被保留或丢弃。

代码语言:javascript
复制
<or>
  <exclude>
    key status_code
    pattern /^5\d\d$/
  </exclude>

  <exclude>
    key url
    pattern /\.css$/
  </exclude>
</or>
代码语言:javascript
复制
<exclude>
  key status_code
  pattern /^5\d\d$/
</exclude>

<exclude>
  key url
  pattern /\.css$/
</exclude>
代码语言:javascript
复制
<or>
  <regexp>
    key container_name
    pattern /^db\d{2}/
  </regexp>

  <regexp>
    key log_level
    pattern /^(?:warn|error)$/
  </regexp>
</or>

我们会在后续文章中对 filter_grep 进行功能测试,以验证其使用方法。

敬请继续关注。

欢迎关注,欢迎转发

谢绝搬运,抄袭必究

本文参与?腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-01-24,如有侵权请联系?cloudcommunity@tencent.com 删除

本文分享自 Fluentd学习交流 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com