Fluentd 的 <match> 如何排除特定模式的日志?
Fluentd 的 match 用通配符匹配 tag,排除特定日志可用取反的标签命名、grep 过滤器的 exclude 块、或 rewrite_tag_filter 把不要的日志路由到 null 输出。本文给出三种排除写法。
<match> 本身只支持通配符(app.*、**)不支持"取反"——排除要用这三招:grep 过滤器的 exclude、rewrite_tag_filter 路由到 @type null、或者把宽泛的 ** match 放在具体 match 之后兜底。
方法一:grep 的 exclude(按内容排除)
<filter app.**>
@type grep
<exclude>
key message
pattern healthcheck|heartbeat
</exclude>
</filter>
message 含 healthcheck 或 heartbeat 的日志被丢弃,其余继续。
方法二:路由到 null(按 tag 排除)
<match debug.**>
@type null # 直接丢弃
</match>
配合 rewrite_tag_filter 先按内容改写 tag,再整体丢弃这一类。
方法三:match 顺序兜底
Fluentd 按配置文件自上而下匹配,先写具体规则、宽泛的 ** 放最后:
<match app.system.**> ... </match> # 先命中
<match app.**> ... </match> # 其余 app 日志
观测云对照
观测云 DataKit Pipeline 用 if ... { drop() } 一行实现排除,条件可组合字段、正则与多个逻辑运算,比在 Fluentd 里绕 tag 路由直观得多。
常见问题(FAQ)
Q:exclude 的正则是匹配整行吗? 部分匹配即可(不锚定);要整行匹配用 ^...$。
Q:丢弃的日志有统计吗? Fluentd 的 monitor_agent 可看到各插件的事件计数。