Fluentd 的 <match> 如何排除特定模式的日志?
Fluentd 的 match 支持通配符,1.11.2 起也支持正则 tag 模式;排除日志可用正则负向前瞻、grep 的 exclude,或改写 tag 后路由到 null 输出。本文给出三种排除写法。
<match> 支持通配符(app.*、**),Fluentd 1.11.2 起还支持 /.../ 正则 tag 模式,可用负向前瞻实现排除。也可用这三招: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 日志
常见问题(FAQ)
Q:exclude 的正则是匹配整行吗? 部分匹配即可(不锚定);^...$ 是行边界;字段含换行时若要完整字符串匹配,应使用 Ruby 的 \A...\z。
Q:丢弃的日志有统计吗? monitor_agent 提供插件暴露的指标,但并非每个插件都提供丢弃计数;需核实所用版本和插件,必要时给丢弃路径配置单独计数。